Skip to content

Instantly share code, notes, and snippets.

@mattdesl
Last active August 29, 2015 14:05
Show Gist options
  • Save mattdesl/3b09b62ab6d64bbfde6c to your computer and use it in GitHub Desktop.
Save mattdesl/3b09b62ab6d64bbfde6c to your computer and use it in GitHub Desktop.
requirebin sketch
var test = require('canvas-testbed')(render)
var time = 0
function render(ctx, width, height, dt) {
ctx.clearRect(0,0,width,height)
time += dt
var smile = 50
var speed = 2
var amt = Math.sin(time/1000 * speed) * smile
ctx.beginPath()
var x = 50, y = 50, width = 70
ctx.moveTo(x, y)
ctx.quadraticCurveTo(x+width/2, y + amt, x + width, y)
ctx.stroke()
}
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({WPSncU:[function(require,module,exports){var domready=require("domready");require("raf.js");var CanvasApp=require("canvas-app");module.exports=function(render,start,options){domready(function(){if(typeof render==="object"&&render){options=render;render=null;start=null}else if(typeof start==="object"&&start){options=start;start=null}options=options||{};if(typeof options.onReady!=="function")options.onReady=start;var runner=CanvasApp(render,options);runner.canvas.setAttribute("id","canvas");document.body.appendChild(runner.canvas);document.body.style.margin="0";document.body.style.overflow="hidden";runner.start()})}},{"canvas-app":3,domready:5,"raf.js":6}],"canvas-testbed":[function(require,module,exports){module.exports=require("WPSncU")},{}],3:[function(require,module,exports){var getGL=require("webgl-context");function CanvasApp(render,options){if(!(this instanceof CanvasApp))return new CanvasApp(render,options);if(typeof render==="object"&&render){options=render;render=null}render=typeof render==="function"?render:options.onRender;options=options||{};options.retina=typeof options.retina==="boolean"?options.retina:true;var hasWidth=typeof options.width==="number",hasHeight=typeof options.height==="number";if(hasWidth||hasHeight)options.ignoreResize=true;options.width=hasWidth?options.width:window.innerWidth;options.height=hasHeight?options.height:window.innerHeight;var DPR=options.retina?window.devicePixelRatio||1:1;var canvas=options.canvas||document.createElement("canvas");canvas.width=options.width*DPR;canvas.height=options.height*DPR;if(options.retina){canvas.style.width=options.width+"px";canvas.style.height=options.height+"px"}var context,attribs=options.contextAttributes||{};this.isWebGL=false;if(options.context==="webgl"||options.context==="experimental-webgl"){context=getGL({canvas:canvas,attributes:attribs});if(!context){throw"WebGL Context Not Supported -- try enabling it or using a different browser"}this.isWebGL=true}else{context=canvas.getContext(options.context||"2d",attribs)}this.running=false;this.width=options.width;this.height=options.height;this.canvas=canvas;this.context=context;this.onResize=options.onResize;this._DPR=DPR;this._retina=options.retina;this._once=options.once;this._ignoreResize=options.ignoreResize;this._lastFrame=null;this._then=Date.now();this.fps=60;this._frames=0;this._prevTime=this._then;if(!this._ignoreResize){window.addEventListener("resize",function(){this.resize(window.innerWidth,window.innerHeight)}.bind(this));window.addEventListener("orientationchange",function(){this.resize(window.innerWidth,window.innerHeight)}.bind(this))}if(typeof render==="function"){this.onRender=render.bind(this)}else{this.onRender=function(context,width,height,dt){}}this.renderOnce=function(){var now=Date.now();var dt=now-this._then;this._frames++;if(now>this._prevTime+1e3){this.fps=Math.round(this._frames*1e3/(now-this._prevTime));this._prevTime=now;this._frames=0}if(!this.isWebGL){this.context.save();this.context.scale(this._DPR,this._DPR)}this.onRender(this.context,this.width,this.height,dt);if(!this.isWebGL)this.context.restore();this._then=now};this._renderHandler=function(){if(!this.running)return;if(!this._once){this._lastFrame=requestAnimationFrame(this._renderHandler)}this.renderOnce()}.bind(this);if(typeof options.onReady==="function"){options.onReady.call(this,context,this.width,this.height)}}Object.defineProperty(CanvasApp.prototype,"retinaEnabled",{set:function(v){this._retina=v;this._DPR=this._retina?window.devicePixelRatio||1:1;this.resize(this.width,this.height)},get:function(){return this._retina}});CanvasApp.prototype.resetFPS=function(){this._frames=0;this._prevTime=Date.now();this._then=this._prevTime;this.fps=60};CanvasApp.prototype.start=function(){if(this.running)return;if(this._lastFrame)cancelAnimationFrame(this._lastFrame);this.resetFPS();this.running=true;this._lastFrame=requestAnimationFrame(this._renderHandler)};CanvasApp.prototype.stop=function(){if(this._lastFrame){cancelAnimationFrame(this._lastFrame);this._lastFrame=null}this.running=false};CanvasApp.prototype.resize=function(width,height){var canvas=this.canvas;this.width=width;this.height=height;canvas.width=this.width*this._DPR;canvas.height=this.height*this._DPR;if(this._retina){canvas.style.width=this.width+"px";canvas.style.height=this.height+"px"}if(this._once)requestAnimationFrame(this._renderHandler);if(typeof this.onResize==="function")this.onResize(this.width,this.height)};module.exports=CanvasApp},{"webgl-context":4}],4:[function(require,module,exports){module.exports=function(opts){opts=opts||{};var canvas=opts.canvas||document.createElement("canvas");if(typeof opts.width==="number")canvas.width=opts.width;if(typeof opts.height==="number")canvas.height=opts.height;var attribs=opts.attributes||opts.attribs||{};try{gl=canvas.getContext("webgl",attribs)||canvas.getContext("experimental-webgl",attribs)}catch(e){gl=null}return gl}},{}],5:[function(require,module,exports){!function(name,definition){if(typeof module!="undefined")module.exports=definition();else if(typeof define=="function"&&typeof define.amd=="object")define(definition);else this[name]=definition()}("domready",function(){var fns=[],listener,doc=document,domContentLoaded="DOMContentLoaded",loaded=/^loaded|^i|^c/.test(doc.readyState);if(!loaded)doc.addEventListener(domContentLoaded,listener=function(){doc.removeEventListener(domContentLoaded,listener);loaded=1;while(listener=fns.shift())listener()});return function(fn){loaded?fn():fns.push(fn)}})},{}],6:[function(require,module,exports){(function(window){var lastTime=0,vendors=["webkit","moz"],requestAnimationFrame=window.requestAnimationFrame,cancelAnimationFrame=window.cancelAnimationFrame,i=vendors.length;while(--i>=0&&!requestAnimationFrame){requestAnimationFrame=window[vendors[i]+"RequestAnimationFrame"];cancelAnimationFrame=window[vendors[i]+"CancelAnimationFrame"]}if(!requestAnimationFrame||!cancelAnimationFrame){requestAnimationFrame=function(callback){var now=+new Date,nextTime=Math.max(lastTime+16,now);return setTimeout(function(){callback(lastTime=nextTime)},nextTime-now)};cancelAnimationFrame=clearTimeout}window.requestAnimationFrame=requestAnimationFrame;window.cancelAnimationFrame=cancelAnimationFrame})(window)},{}]},{},[]);var test=require("canvas-testbed")(render);var time=0;function render(ctx,width,height,dt){ctx.clearRect(0,0,width,height);time+=dt;var smile=50;var speed=2;var amt=Math.sin(time/1e3*speed)*smile;ctx.beginPath();var x=50,y=50,width=70;ctx.moveTo(x,y);ctx.quadraticCurveTo(x+width/2,y+amt,x+width,y);ctx.stroke()}
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"canvas-testbed": "0.4.0"
}
}
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; }
body, html { height: 100%; width: 100%; }</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment