From 7c02eeeffce99aeb5ad65e4eb26ab2fc3fcf4aa9 Mon Sep 17 00:00:00 2001 From: Georg Fischer Date: Thu, 27 Jun 2013 15:21:32 +0200 Subject: [PATCH] add canvas helper file add request animation frame --- scripts/aux/canvas.js | 38 ++++++++++++++++++++++++++++++++++ scripts/aux/glitch.js | 26 ++++-------------------- scripts/src/process.js | 46 +++++++++++++++++++++++------------------- 3 files changed, 67 insertions(+), 43 deletions(-) create mode 100644 scripts/aux/canvas.js diff --git a/scripts/aux/canvas.js b/scripts/aux/canvas.js new file mode 100644 index 0000000..f7fe2dd --- /dev/null +++ b/scripts/aux/canvas.js @@ -0,0 +1,38 @@ +/*global define*/ +define( + function() + { + var update = false; + + function resize( canvas, size ) + { + + if ( canvas.width !== size.width ) + { + canvas.width = size.width; + update = true; + } + + if ( canvas.height !== size.height ) + { + canvas.height = size.height; + update = true; + } + + if ( update ) + { + canvas.width = size.width; + canvas.height = size.height; + } + + update = false; + } + + function clear( canvas, ctx ) + { + ctx.clearRect( ctx, 0, 0, canvas.width, canvas.height ); + } + + return { resize: resize, clear: clear }; + } +); \ No newline at end of file diff --git a/scripts/aux/glitch.js b/scripts/aux/glitch.js index df5ec58..3dcfafe 100644 --- a/scripts/aux/glitch.js +++ b/scripts/aux/glitch.js @@ -1,6 +1,7 @@ /*global define*/ define( - function() + [ 'aux/canvas' ], + function( canvas_helper ) { var canvas = document.createElement( 'canvas' ); var ctx = canvas.getContext( '2d' ); @@ -36,7 +37,8 @@ define( offset = input.offset / 100; iterations = input.iterations; - updateCanvasSize( image_data.width, image_data.height ); + canvas_helper.resize( canvas, image_data ); + canvas_helper.resize( tmp_canvas, image_data ); base64 = getBase64FromImageData( image_data, quality ); byte_array = base64ToByteArray( base64 ); @@ -57,26 +59,6 @@ define( img.src = byteArrayToBase64( byte_array ); } - function updateCanvasSize( width, height ) - { - var updated = false; - - if ( canvas_size.width !== width ) { canvas_size.width = width; updated = true; } - if ( canvas_size.height !== height ) { canvas_size.height = height; updated = true; } - - if ( updated ) - { - resizeCanvas( tmp_canvas, canvas_size ); - resizeCanvas( canvas, canvas_size ); - } - } - - function resizeCanvas( canvas, img ) - { - canvas.width = img.width; - canvas.height = img.height; - } - function glitchJpegBytes( byte_array, jpg_header_length, seed, offset, iteration ) { var it = ( iteration + 1 ) / 10; diff --git a/scripts/src/process.js b/scripts/src/process.js index f103476..e2a96b2 100644 --- a/scripts/src/process.js +++ b/scripts/src/process.js @@ -1,7 +1,7 @@ -/*global define*/ +/*global define, requestAnimationFrame*/ define( - [ 'aux/glitch' ], - function( glitch ) + [ 'aux/glitch', 'aux/canvas', 'lib/raf' ], + function( glitch, canvas_helper ) { var tmp_canvas = document.createElement( 'canvas' ); var tmp_ctx = tmp_canvas.getContext( '2d' ); @@ -14,6 +14,7 @@ define( var image; var signals; var image_data; + var canvas_size; function init( shared ) { @@ -40,23 +41,37 @@ define( } } + function requestTick() + { + if ( ! is_processing ) + { + requestAnimationFrame( update ); + } + + is_processing = true; + } + function update() { - if ( ! is_processing && image ) + if ( image ) { processImage( image ); } + + else + { + is_processing = false; + } } function processImage( img ) { is_processing = true; - clearCanvas( tmp_canvas, tmp_ctx ); - clearCanvas( canvas, ctx ); - - resizeCanvas( tmp_canvas, img ); - resizeCanvas( canvas, img ); + canvas_helper.clear( tmp_canvas, tmp_ctx ); + canvas_helper.clear( canvas, ctx ); + canvas_helper.resize( tmp_canvas, img ); + canvas_helper.resize( canvas, img ); tmp_ctx.drawImage( img, 0, 0 ); @@ -67,24 +82,13 @@ define( function draw( image_data ) { - resizeCanvas( canvas, image_data ); + canvas_helper.resize( canvas, image_data ); ctx.putImageData( image_data, 0, 0 ); is_processing = false; image_data = null; } - function resizeCanvas( canvas, img ) - { - canvas.width = img.width; - canvas.height = img.height; - } - - function clearCanvas( canvas, ctx ) - { - ctx.clearRect( ctx, 0, 0, canvas.width, canvas.height ); - } - function exportData() { signals['export-png'].dispatch( canvas.toDataURL( 'image/png' ) );