Merge pull request #63 from cobexer/minification-improvements

Minification improvements
This commit is contained in:
Niklas von Hertzen 2012-02-27 11:09:26 -08:00
commit f485028d30
13 changed files with 86 additions and 52 deletions

3
.gitignore vendored
View File

@ -5,8 +5,7 @@
/tests/cache/ /tests/cache/
/tests/flashcanvas.html /tests/flashcanvas.html
/lib/ /lib/
/dist/ /build/
/build/*.js
index.html index.html
image.jpg image.jpg
screenshots.html screenshots.html

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml version="1.0" encoding="utf-8"?>
<project name="html2canvas" basedir="." default="build"> <project name="html2canvas" basedir="." default="build">
<property name="src.dir" location="src"/> <property name="src.dir" location="src"/>
<property name="lib.dir" location="../lib"/> <property name="lib.dir" location="../lib"/>
@ -11,44 +11,56 @@
<property name="JQUERY_PLUGIN_NAME" value="jquery.plugin.html2canvas.js"/> <property name="JQUERY_PLUGIN_NAME" value="jquery.plugin.html2canvas.js"/>
<loadfile property="version" srcfile="version.txt" /> <loadfile property="version" srcfile="version.txt" />
<fileset id="sourcefiles" dir="${src.dir}"> <path id="sourcefiles">
<include name="LICENSE"/> <filelist dir="${src.dir}">
<include name="Core.js"/> <file name="LICENSE"/>
<include name="Generate.js"/> <file name="html2canvas-pre.txt"/>
<include name="Parse.js"/> <file name="Core.js"/>
<include name="Preload.js"/> <file name="Generate.js"/>
<include name="Queue.js"/> <file name="Parse.js"/>
<include name="Renderer.js"/> <file name="Preload.js"/>
</fileset> <file name="Queue.js"/>
<file name="Renderer.js"/>
<file name="Util.js"/>
<file name="html2canvas-post.txt"/>
</filelist>
</path>
<path id="jquery-plugin"> <path id="jquery-plugin">
<fileset dir="${src.dir}" includes="LICENSE"/> <fileset dir="${src.dir}" includes="LICENSE"/>
<fileset dir="${src.dir}/plugins" includes="${JQUERY_PLUGIN_NAME}"/> <fileset dir="${src.dir}/plugins" includes="${JQUERY_PLUGIN_NAME}"/>
</path> </path>
<target name="plugins"> <target name="build-dir">
<echo>Creating directory ${build.dir}...</echo>
<mkdir dir="${build.dir}"/>
</target>
<target name="plugins" depends="build-dir">
<echo>Creating ${JQUERY_PLUGIN_NAME}...</echo>
<concat fixlastline="yes" destfile="${build.dir}/${JQUERY_PLUGIN_NAME}"> <concat fixlastline="yes" destfile="${build.dir}/${JQUERY_PLUGIN_NAME}">
<path refid="jquery-plugin"/> <path refid="jquery-plugin"/>
</concat> </concat>
<replaceregexp match="@VERSION@" replace="${version}" flags="g" byline="true" file="${build.dir}/${JQUERY_PLUGIN_NAME}" /> <replaceregexp match="@VERSION@" replace="${version}" flags="g" byline="true" file="${build.dir}/${JQUERY_PLUGIN_NAME}" />
</target> </target>
<pathconvert property="prettty-sourcefiles" pathsep="${line.separator}" refid="sourcefiles"></pathconvert>
<target name="build" depends="plugins"> <target name="build" depends="build-dir,plugins">
<echo>Concatenating files:${line.separator}${prettty-sourcefiles}${line.separator}into ${build.dir}/${JS_NAME}...</echo>
<concat fixlastline="yes" destfile="${build.dir}/${JS_NAME}"> <concat fixlastline="yes" destfile="${build.dir}/${JS_NAME}">
<fileset refid="sourcefiles"/> <path refid="sourcefiles"/>
</concat> </concat>
<replaceregexp match="@VERSION@" replace="${version}" flags="g" byline="true" file="${build.dir}/${JS_NAME}" /> <replaceregexp match="@VERSION@" replace="${version}" flags="g" byline="true" file="${build.dir}/${JS_NAME}" />
</target> </target>
<taskdef name="jscomp" classname="com.google.javascript.jscomp.ant.CompileTask" <taskdef name="jscomp" classname="com.google.javascript.jscomp.ant.CompileTask"
classpath="${lib.dir}/compiler.jar" onerror="report"/> classpath="${lib.dir}/compiler.jar" onerror="report"/>
<target name="release" depends="build"> <target name="syntaxcheck" depends="build-dir,build">
<jscomp compilationLevel="simple" warning="verbose" <jscomp compilationLevel="simple" warning="verbose"
debug="false" debug="false"
output="${build.dir}/${JS_NAME_MIN}"> output="${build.dir}/${JS_NAME_MIN}.tmp">
<externs dir="${lib.dir}"> <externs dir="${lib.dir}">
<file name="${jquery-externs}"/> <file name="${jquery-externs}"/>
</externs> </externs>
@ -61,15 +73,28 @@
<file name="Preload.js"/> <file name="Preload.js"/>
<file name="Queue.js"/> <file name="Queue.js"/>
<file name="Renderer.js"/> <file name="Renderer.js"/>
<file name="Util.js"/>
</sources>
</jscomp>
<delete file="${build.dir}/${JS_NAME_MIN}.tmp"></delete>
</target>
<target name="release" depends="build-dir,build,syntaxcheck">
<jscomp compilationLevel="simple" warning="verbose"
debug="false"
output="${build.dir}/${JS_NAME_MIN}">
<externs dir="${lib.dir}">
<file name="${jquery-externs}"/>
</externs>
<sources dir="${build.dir}">
<file name="${JS_NAME}"/>
</sources> </sources>
</jscomp> </jscomp>
<replaceregexp match="@VERSION@" replace="${version}" flags="g" byline="true" file="${build.dir}/${JS_NAME_MIN}" /> <replaceregexp match="@VERSION@" replace="${version}" flags="g" byline="true" file="${build.dir}/${JS_NAME_MIN}" />
</target> </target>
<target name="clean"> <target name="clean">
<delete file="${build.dir}/${JS_NAME}"></delete> <delete dir="${build.dir}"></delete>
<delete file="${build.dir}/${JS_NAME_MIN}"></delete>
<delete file="${build.dir}/${JQUERY_PLUGIN_NAME}"></delete>
</target> </target>
</project> </project>

View File

View File

@ -37,10 +37,15 @@ For more information and examples, please visit the <a href="http://html2canvas.
### Changelog ### ### Changelog ###
v0.33 -
* Improved minification saved ~1K! (<a href="https://github.com/cobexer/html2canvas/commit/b82be022b2b9240bd503e078ac980bde2b953e43">cobexer</a>)
v0.32 - 20.2.2012 v0.32 - 20.2.2012
* Added changelog! * Added changelog!
* Added bookmarklet (<a href="https://github.com/niklasvh/html2canvas/commit/b320dd306e1a2d32a3bc5a71b6ebf6d8c060cde5">cobexer</a>) * Added bookmarklet (<a href="https://github.com/niklasvh/html2canvas/commit/b320dd306e1a2d32a3bc5a71b6ebf6d8c060cde5">cobexer</a>)
* Option to select single element to render (<a href="https://github.com/niklasvh/html2canvas/commit/0cb252ada91c84ef411288b317c03e97da1f12ad">niklasvh</a>) * Option to select single element to render (<a href="https://github.com/niklasvh/html2canvas/commit/0cb252ada91c84ef411288b317c03e97da1f12ad">niklasvh</a>)
* Fixed closure compiler warnings (<a href="https://github.com/niklasvh/html2canvas/commit/36ff1ec7aadcbdf66851a0b77f0b9e87e4a8e4a1">cobexer</a>) * Fixed closure compiler warnings (<a href="https://github.com/niklasvh/html2canvas/commit/36ff1ec7aadcbdf66851a0b77f0b9e87e4a8e4a1">cobexer</a>)
* Enable profiling in FF (<a href="https://github.com/niklasvh/html2canvas/commit/bbd75286a8406cf9e5aea01fdb7950d547edefb9">cobexer</a>) * Enable profiling in FF (<a href="https://github.com/niklasvh/html2canvas/commit/bbd75286a8406cf9e5aea01fdb7950d547edefb9">cobexer</a>)

View File

@ -10,11 +10,13 @@ var html2canvas = {};
html2canvas.logging = false; html2canvas.logging = false;
html2canvas.log = function (a) { function h2clog(a) {
if (html2canvas.logging && window.console && window.console.log) { if (html2canvas.logging && window.console && window.console.log) {
window.console.log(a); window.console.log(a);
} }
}; }
html2canvas.log = h2clog; // for compatibility with the jquery plugin
html2canvas.Util = {}; html2canvas.Util = {};
@ -140,8 +142,8 @@ html2canvas.Util.Children = function(el) {
try { try {
children = $(el).contents(); children = $(el).contents();
} catch (ex) { } catch (ex) {
html2canvas.log("html2canvas.Util.Children failed with exception: " + ex.message); h2clog("html2canvas.Util.Children failed with exception: " + ex.message);
children = []; children = [];
} }
return children; return children;
} };

View File

@ -108,7 +108,7 @@ html2canvas.Generate.Gradient = function(src, bounds) {
lingrad.addColorStop(increment * i, steps[i]); lingrad.addColorStop(increment * i, steps[i]);
} }
catch(e) { catch(e) {
html2canvas.log(['failed to add color stop: ', e, '; tried to add: ', steps[i], '; stop: ', i, '; in: ', src]); h2clog(['failed to add color stop: ', e, '; tried to add: ', steps[i], '; stop: ', i, '; in: ', src]);
} }
} }

View File

@ -1002,7 +1002,7 @@ html2canvas.Parse = function (element, images, opts) {
} }
}else{ }else{
html2canvas.log("html2canvas: Error loading background:" + background_image); h2clog("html2canvas: Error loading background:" + background_image);
//console.log(images); //console.log(images);
} }
@ -1051,7 +1051,7 @@ html2canvas.Parse = function (element, images, opts) {
stack = { stack = {
ctx: html2canvas.canvasContext( docDim.width || w , docDim.height || h ), ctx: h2cRenderContext( docDim.width || w , docDim.height || h ),
zIndex: zindex, zIndex: zindex,
opacity: opacity * parentStack.opacity, opacity: opacity * parentStack.opacity,
cssPosition: cssPosition cssPosition: cssPosition
@ -1162,7 +1162,7 @@ html2canvas.Parse = function (element, images, opts) {
); );
}else{ }else{
html2canvas.log("html2canvas: Error loading <img>:" + imgSrc); h2clog("html2canvas: Error loading <img>:" + imgSrc);
} }
break; break;
case "INPUT": case "INPUT":

View File

@ -45,7 +45,7 @@ html2canvas.Preload = function(element, opts){
} }
function start(){ function start(){
html2canvas.log("html2canvas: start: images: " + images.numLoaded + " / " + images.numTotal + " (failed: " + images.numFailed + ")"); h2clog("html2canvas: start: images: " + images.numLoaded + " / " + images.numTotal + " (failed: " + images.numFailed + ")");
if (!images.firstRun && images.numLoaded >= images.numTotal){ if (!images.firstRun && images.numLoaded >= images.numTotal){
/* /*
@ -57,7 +57,7 @@ html2canvas.Preload = function(element, opts){
options.complete(images); options.complete(images);
} }
html2canvas.log("Finished loading images: # " + images.numTotal + " (failed: " + images.numFailed + ")"); h2clog("Finished loading images: # " + images.numTotal + " (failed: " + images.numFailed + ")");
} }
} }
@ -169,7 +169,7 @@ html2canvas.Preload = function(element, opts){
elNodeType = el.nodeType; elNodeType = el.nodeType;
} catch (ex) { } catch (ex) {
elNodeType = false; elNodeType = false;
html2canvas.log("html2canvas: failed to access some element's nodeType - Exception: " + ex.message); h2clog("html2canvas: failed to access some element's nodeType - Exception: " + ex.message);
} }
if (elNodeType === 1 || elNodeType === undefined){ if (elNodeType === 1 || elNodeType === undefined){
@ -256,9 +256,9 @@ html2canvas.Preload = function(element, opts){
var img, src; var img, src;
if (!images.cleanupDone) { if (!images.cleanupDone) {
if (cause && typeof cause === "string") { if (cause && typeof cause === "string") {
html2canvas.log("html2canvas: Cleanup because: " + cause); h2clog("html2canvas: Cleanup because: " + cause);
} else { } else {
html2canvas.log("html2canvas: Cleanup after timeout: " + options.timeout + " ms."); h2clog("html2canvas: Cleanup after timeout: " + options.timeout + " ms.");
} }
for (src in images) { for (src in images) {
@ -276,7 +276,7 @@ html2canvas.Preload = function(element, opts){
} }
images.numLoaded++; images.numLoaded++;
images.numFailed++; images.numFailed++;
html2canvas.log("html2canvas: Cleaned up failed img: '" + src + "' Steps: " + images.numLoaded + " / " + images.numTotal); h2clog("html2canvas: Cleaned up failed img: '" + src + "' Steps: " + images.numLoaded + " / " + images.numTotal);
} }
} }
} }
@ -307,19 +307,19 @@ html2canvas.Preload = function(element, opts){
if (options.timeout > 0) { if (options.timeout > 0) {
timeoutTimer = window.setTimeout(methods.cleanupDOM, options.timeout); timeoutTimer = window.setTimeout(methods.cleanupDOM, options.timeout);
} }
html2canvas.log('html2canvas: Preload starts: finding background-images'); h2clog('html2canvas: Preload starts: finding background-images');
images.firstRun = true; images.firstRun = true;
getImages( element ); getImages( element );
html2canvas.log('html2canvas: Preload: Finding images'); h2clog('html2canvas: Preload: Finding images');
// load <img> images // load <img> images
for (i = 0; i < imgLen; i+=1){ for (i = 0; i < imgLen; i+=1){
methods.loadImage( domImages[i].getAttribute( "src" ) ); methods.loadImage( domImages[i].getAttribute( "src" ) );
} }
images.firstRun = false; images.firstRun = false;
html2canvas.log('html2canvas: Preload: Done.'); h2clog('html2canvas: Preload: Done.');
if ( images.numTotal === images.numLoaded ) { if ( images.numTotal === images.numLoaded ) {
start(); start();
} }

View File

@ -5,7 +5,7 @@
Released under MIT License Released under MIT License
*/ */
html2canvas.canvasContext = function (width, height) { function h2cRenderContext(width, height) {
var storage = []; var storage = [];
return { return {
storage: storage, storage: storage,
@ -40,4 +40,4 @@ html2canvas.canvasContext = function (width, height) {
}); });
} }
}; };
}; }

View File

@ -176,7 +176,7 @@ html2canvas.Renderer = function(parseQueue, opts){
} }
html2canvas.log("html2canvas: Renderer: Canvas renderer done - returning canvas obj"); h2clog("html2canvas: Renderer: Canvas renderer done - returning canvas obj");
// this.canvasRenderStorage(queue,this.ctx); // this.canvasRenderStorage(queue,this.ctx);
queueLen = options.elements.length; queueLen = options.elements.length;
@ -390,7 +390,7 @@ html2canvas.Renderer = function(parseQueue, opts){
html2canvas.log("html2canvas: Renderer: SVG Renderer done - returning SVG DOM obj"); h2clog("html2canvas: Renderer: SVG Renderer done - returning SVG DOM obj");
return svg; return svg;
@ -405,11 +405,11 @@ html2canvas.Renderer = function(parseQueue, opts){
case "canvas": case "canvas":
canvas = doc.createElement('canvas'); canvas = doc.createElement('canvas');
if (canvas.getContext){ if (canvas.getContext){
html2canvas.log("html2canvas: Renderer: using canvas renderer"); h2clog("html2canvas: Renderer: using canvas renderer");
return canvasRenderer(parseQueue); return canvasRenderer(parseQueue);
} else { } else {
usingFlashcanvas = true; usingFlashcanvas = true;
html2canvas.log("html2canvas: Renderer: canvas not available, using flashcanvas"); h2clog("html2canvas: Renderer: canvas not available, using flashcanvas");
var script = doc.createElement("script"); var script = doc.createElement("script");
script.src = options.flashcanvas; script.src = options.flashcanvas;
@ -436,7 +436,7 @@ html2canvas.Renderer = function(parseQueue, opts){
window.setTimeout( intervalFunc, 250 ); window.setTimeout( intervalFunc, 250 );
} else { } else {
html2canvas.log("html2canvas: Renderer: Can't track when flashcanvas is loaded"); h2clog("html2canvas: Renderer: Can't track when flashcanvas is loaded");
} }
@ -446,9 +446,9 @@ html2canvas.Renderer = function(parseQueue, opts){
})(script, function(){ })(script, function(){
if (typeof FlashCanvas !== "undefined") { if (typeof window.FlashCanvas !== "undefined") {
html2canvas.log("html2canvas: Renderer: Flashcanvas initialized"); h2clog("html2canvas: Renderer: Flashcanvas initialized");
FlashCanvas.initElement( canvas ); window.FlashCanvas.initElement( canvas );
canvasRenderer(parseQueue); canvasRenderer(parseQueue);
} }
}); });
@ -460,7 +460,7 @@ html2canvas.Renderer = function(parseQueue, opts){
break; break;
case "svg": case "svg":
if (doc.createElementNS){ if (doc.createElementNS){
html2canvas.log("html2canvas: Renderer: using SVG renderer"); h2clog("html2canvas: Renderer: using SVG renderer");
return svgRenderer(parseQueue); return svgRenderer(parseQueue);
} }
break; break;

2
src/html2canvas-post.txt Normal file
View File

@ -0,0 +1,2 @@
window.html2canvas = html2canvas;
}(window, document));

1
src/html2canvas-pre.txt Normal file
View File

@ -0,0 +1 @@
(function(window, document, undefined){

View File

@ -8,7 +8,7 @@
(function(document, window) { (function(document, window) {
var scrStart = '<script type="text/javascript" src="', scrEnd = '"></script>'; var scrStart = '<script type="text/javascript" src="', scrEnd = '"></script>';
document.write(scrStart + '../external/jquery-1.6.2.js' + scrEnd); document.write(scrStart + '../external/jquery-1.6.2.js' + scrEnd);
var html2canvas = ['Core', 'Generate', 'Parse', 'Preload', 'Queue', 'Renderer', 'plugins/jquery.plugin.html2canvas'], i; var html2canvas = ['Core', 'Generate', 'Parse', 'Preload', 'Queue', 'Renderer', 'Util', 'plugins/jquery.plugin.html2canvas'], i;
for (i = 0; i < html2canvas.length; ++i) { for (i = 0; i < html2canvas.length; ++i) {
document.write(scrStart + '../src/' + html2canvas[i] + '.js' + scrEnd); document.write(scrStart + '../src/' + html2canvas[i] + '.js' + scrEnd);
} }