Skip to content

Commit

Permalink
Separate chaining method that adds a chained set method for passing…
Browse files Browse the repository at this point in the history
… key, value pairs or a hash of properties to set. Updated drawing example to illustrate this. #18
  • Loading branch information
Justin Windle committed May 28, 2013
2 parents b02b9cb + 07834e1 commit 2bd1566
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
18 changes: 11 additions & 7 deletions examples/drawing.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ <h3>Start drawing!</h3>

touch = this.touches[i];

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

// Draw using chained methods
this.beginPath()
this
// Setup multiple properties using a hash
.set({
lineCap: 'round',
lineJoin: 'round',
lineWidth: radius
})
// ...or set one by passing the property name followed by the value
.set( 'fillStyle', this.strokeStyle = COLOURS[ i % COLOURS.length ] )
// Chain function calls together
.beginPath()
.moveTo( touch.ox, touch.oy )
.lineTo( touch.x, touch.y )
.stroke();
Expand Down
2 changes: 1 addition & 1 deletion examples/three.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h2>Using a THREE.js renderer context with sketch.js</h2>
<a href="https://github.com/soulwire/sketch.js" target="_blank">View on Github</a>
</nav>
</header>
<script src="https://raw.github.com/mrdoob/three.js/r58/build/three.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r58/three.min.js"></script>
<script src="../js/sketch.js"></script>
<script>

Expand Down
31 changes: 25 additions & 6 deletions js/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,44 @@ var Sketch = (function() {
};
}

function clone( target, chain ) {
function clone( target ) {

var result = chain ? target : {};
var result = {};

for ( var key in target ) {

if ( isFunction( target[ key ] ) )

result[ key ] = proxy( target[ key ], target );

else if ( !chain )
else

result[ key ] = target[ key ];
}

return result;
}

function chain( target ) {

extend( target, {

set: function( key, val ) {

if ( isString( key ) ) target[ key ] = val;
else extend( target, key, true );
}
});

for ( var key in target )

if ( isFunction( target[ key ] ) )

target[ key ] = proxy( target[ key ], target );

return target;
}

/*
----------------------------------------------------------------------
Expand Down Expand Up @@ -430,6 +450,7 @@ var Sketch = (function() {
var element, context, Sketch = {

CANVAS: CANVAS,
WEB_GL: WEBGL,
WEBGL: WEBGL,
DOM: DOM,

Expand Down Expand Up @@ -513,9 +534,7 @@ var Sketch = (function() {

options = extend( options || {}, defaults );

if ( options.chain )

clone( context, true );
if ( options.chain ) chain( context );

options.element = context.canvas || context;
options.element.className += ' sketch';
Expand Down
2 changes: 1 addition & 1 deletion js/sketch.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion tests/spec/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,25 @@ describe( 'create', function() {
chain: true
});

sketch.set( 'lineWidth', 10 );
sketch.set({
globalCompositeOperation: 'lighter',
globalAlpha: 0.5,
lineCap: 'round'
});

expect( sketch.lineWidth ).toBe( 10 );
expect( sketch.globalCompositeOperation ).toBe( 'lighter' );
expect( sketch.globalAlpha ).toBe( 0.5 );
expect( sketch.lineCap ).toBe( 'round' );
expect( sketch.beginPath() ).toBe( sketch );
expect( sketch.moveTo(0,0) ).toBe( sketch );
expect( sketch.lineTo(0,0) ).toBe( sketch );
expect( sketch.closePath() ).toBe( sketch );
expect( sketch.stroke() ).toBe( sketch );
expect( sketch.fill() ).toBe( sketch );
expect( sketch.fillRect(0,0,1,1) ).toBe( sketch );
expect( sketch.isPointInPath(0,0) ).toBe( false );
expect( sketch.isPointInPath(10,10) ).toBe( false );
expect( sketch.getImageData(0,0,1,1) ).toEqual( jasmine.any( ImageData ) );
});

Expand Down

0 comments on commit 2bd1566

Please sign in to comment.