Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dom ready issue #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 48 additions & 46 deletions examples/drawing.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,70 @@
background: #F51E50;
}
</style>
</head>
<body>
<div id="container"></div>
<header class="info">
<hgroup class="about">
<h1>sketch.js &rsaquo; basic</h1>
<h2>A minimal example of how to use sketch.js</h2>
<h3>Start drawing!</h3>
</hgroup>
<nav class="more">
<a href="https://github.com/soulwire/sketch.js/archive/master.zip" target="_blank">Download</a>
<a href="https://github.com/soulwire/sketch.js" target="_blank">View on Github</a>
</nav>
</header>
<script src="../js/sketch.js"></script>
<script>

var COLOURS = [ '#E3EB64', '#A7EBCA', '#FFFFFF', '#D8EBA7', '#868E80' ];
var radius = 0;

Sketch.create({
window.addEventListener('DOMContentLoaded', run);

function run() {
Sketch.create({

container: document.getElementById( 'container' ),
autoclear: false,
container: document.getElementById( 'container' ),
autoclear: false,

setup: function() {
console.log( 'setup' );
},
setup: function() {
console.log( 'setup' );
},

update: function() {
radius = 2 + abs( sin( this.millis * 0.003 ) * 50 );
},
update: function() {
radius = 2 + abs( sin( this.millis * 0.003 ) * 50 );
},

// Event handlers
// Event handlers

keydown: function() {
if ( this.keys.C ) this.clear();
},
keydown: function() {
if ( this.keys.C ) this.clear();
},

// Mouse & touch events are merged, so handling touch events by default
// and powering sketches using the touches array is recommended for easy
// scalability. If you only need to handle the mouse / desktop browsers,
// use the 0th touch element and you get wider device support for free.
touchmove: function() {
// Mouse & touch events are merged, so handling touch events by default
// and powering sketches using the touches array is recommended for easy
// scalability. If you only need to handle the mouse / desktop browsers,
// use the 0th touch element and you get wider device support for free.
touchmove: function() {

for ( var i = this.touches.length - 1, touch; i >= 0; i-- ) {
for ( var i = this.touches.length - 1, touch; i >= 0; i-- ) {

touch = this.touches[i];
touch = this.touches[i];

this.lineCap = 'round';
this.lineJoin = 'round';
this.fillStyle = this.strokeStyle = COLOURS[ i % COLOURS.length ];
this.lineWidth = radius;
this.lineCap = 'round';
this.lineJoin = 'round';
this.fillStyle = this.strokeStyle = COLOURS[ i % COLOURS.length ];
this.lineWidth = radius;

this.beginPath();
this.moveTo( touch.ox, touch.oy );
this.lineTo( touch.x, touch.y );
this.stroke();
this.beginPath();
this.moveTo( touch.ox, touch.oy );
this.lineTo( touch.x, touch.y );
this.stroke();
}
}
}
});

});
}
</script>
</head>
<body>
<div id="container"></div>
<header class="info">
<hgroup class="about">
<h1>sketch.js &rsaquo; basic</h1>
<h2>A minimal example of how to use sketch.js</h2>
<h3>Start drawing!</h3>
</hgroup>
<nav class="more">
<a href="https://github.com/soulwire/sketch.js/archive/master.zip" target="_blank">Download</a>
<a href="https://github.com/soulwire/sketch.js" target="_blank">View on Github</a>
</nav>
</header>
</body>
</html>
20 changes: 8 additions & 12 deletions js/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
if ( typeof exports === 'object' ) {

// CommonJS like
module.exports = factory(root, root.document);
module.exports = factory(root);

} else if ( typeof define === 'function' && define.amd ) {

// AMD
define( function() { return factory( root, root.document ); });
define( function() { return factory(root); });

} else {

// Browser global
root.Sketch = factory( root, root.document );
root.Sketch = factory(root);
}

}( this, function ( window, document ) {
}( this, function (win) {

"use strict";

Expand All @@ -39,18 +39,14 @@
var WEBGL = 'webgl';
var DOM = 'dom';

var doc = document;
var win = window;

var instances = [];

var defaults = {

fullscreen: true,
autostart: true,
autoclear: true,
autopause: true,
container: doc.body,
container: false,
interval: 1,
globals: true,
retina: false,
Expand Down Expand Up @@ -178,7 +174,7 @@
pointer, 'mouseout',
pointer, 'mouseover',

doc,
win.document,

keypress, 'keydown', 'keyup',

Expand Down Expand Up @@ -527,7 +523,7 @@

if ( options.globals ) Sketch.install( self );

element = options.element = options.element || doc.createElement( options.type === DOM ? 'div' : 'canvas' );
element = options.element = options.element || win.document.createElement( options.type === DOM ? 'div' : 'canvas' );

context = options.context = options.context || (function() {

Expand All @@ -548,7 +544,7 @@

})();

( options.container || doc.body ).appendChild( element );
( options.container || win.document.body ).appendChild( element );

return Sketch.augment( context, options );
},
Expand Down