Skip to content

Commit d358361

Browse files
author
Michael Bleigh
committed
Adds download functionality.
1 parent 3b6010d commit d358361

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

index.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,21 @@ <h1>Simple Example</h1>
9292
</article>
9393

9494
<article class='example'>
95-
<h1>Changing Color/Size</h1>
95+
<h1>Changing Color/Size, Downloading Image</h1>
9696

9797
<p>Sketch.js makes it very easy to create links that function as tool buttons.
9898
By default, any link that has a <code>href</code> tag that points to the ID
9999
of the converted canvas will automatically be considered a tool link. If the
100100
link has a <code>data-color</code> attribute then clicking it will change the color.
101101
Similarly, a <code>data-size</code> attribute will change the brush size.</p>
102+
103+
<p>Setting the <code>data-download</code> on a tool link to <code>jpeg</code> or
104+
<code>png</code> will create a download button for the specified format.</p>
102105

103106
<div class='demo'>
104-
<div id='colors_tools'></div>
107+
<div id='colors_tools'>
108+
<a href='#colors_sketch' data-download='png' style='float: right; width: 100px;'>Download</a>
109+
</div>
105110
<canvas id='colors_sketch' width='800' height='300'></canvas>
106111
<script type='text/javascript'>
107112
$(function() {

src/sketch.coffee

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ Released under the MIT License
3131
$(this).data('sketch').color = color
3232
@canvas.bind 'sketch.changesize', (e, size)->
3333
$(this).data('sketch').size = size
34+
@canvas.bind 'sketch.download', (e, format)->
35+
$(this).data('sketch').download(format)
36+
3437
@canvas.bind 'mousedown mouseup mousemove mouseleave mouseout touchstart touchmove touchend touchcancel', @onEvent
3538

3639
if @options.toolLinks
@@ -41,6 +44,8 @@ Released under the MIT License
4144
$canvas.trigger 'sketch.changecolor', $(this).attr('data-color')
4245
if $this.attr('data-size')
4346
$canvas.trigger 'sketch.changesize', parseFloat($(this).attr('data-size'))
47+
if $(this).attr('data-download')
48+
$canvas.trigger 'sketch.download', $(this).attr('data-download')
4449
false
4550

4651

@@ -93,6 +98,16 @@ Released under the MIT License
9398
$.sketch.tools[this.tool].draw.call sketch, this
9499
$.sketch.tools[@action.tool].draw.call sketch, @action if @painting && @action
95100

101+
download: (filename, format)->
102+
format or= "png"
103+
format = "jpeg" if format == "jpg"
104+
mime = "image/#{format}"
105+
106+
imgData = @el.toDataURL(mime)
107+
imgData = imgData.replace(mime, "image/octet-stream")
108+
109+
document.location.href = imgData
110+
96111
$.sketch.tools.marker =
97112
draw: (action)->
98113
@context.lineJoin = "round"

src/sketch.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Released under the MIT License
3232
this.canvas.bind('sketch.changesize', function(e, size) {
3333
return $(this).data('sketch').size = size;
3434
});
35+
this.canvas.bind('sketch.download', function(e, format) {
36+
return $(this).data('sketch').download(format);
37+
});
3538
this.canvas.bind('mousedown mouseup mousemove mouseleave mouseout touchstart touchmove touchend touchcancel', this.onEvent);
3639
if (this.options.toolLinks) {
3740
$('body').delegate("a[href=\"#" + (this.canvas.attr('id')) + "\"]", 'click', function(e) {
@@ -44,6 +47,9 @@ Released under the MIT License
4447
if ($this.attr('data-size')) {
4548
$canvas.trigger('sketch.changesize', parseFloat($(this).attr('data-size')));
4649
}
50+
if ($(this).attr('data-download')) {
51+
$canvas.trigger('sketch.download', $(this).attr('data-download'));
52+
}
4753
return false;
4854
});
4955
}
@@ -111,6 +117,17 @@ Released under the MIT License
111117
return $.sketch.tools[this.action.tool].draw.call(sketch, this.action);
112118
}
113119
};
120+
Sketch.prototype.download = function(filename, format) {
121+
var imgData, mime;
122+
format || (format = "png");
123+
if (format === "jpg") {
124+
format = "jpeg";
125+
}
126+
mime = "image/" + format;
127+
imgData = this.el.toDataURL(mime);
128+
imgData = imgData.replace(mime, "image/octet-stream");
129+
return document.location.href = imgData;
130+
};
114131
return Sketch;
115132
})();
116133
$.sketch.tools.marker = {

0 commit comments

Comments
 (0)