Skip to content

Commit 34290b7

Browse files
author
Michael Bleigh
committed
Adds color tools.
1 parent b515a26 commit 34290b7

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

example/colors.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<script src="http://code.jquery.com/jquery-latest.js"></script>
5+
<script src='../src/sketch.js'></script>
6+
<script type='text/javascript'>
7+
$(function() {
8+
$.each(['#f00', '#ff0', '#0f0', '#0ff', '#00f', '#000', '#fff'], function() {
9+
$('#tools').append("<a href='#colored_sketch' data-color='" + this + "' style='border: 1px solid black; width: 30px; height: 30px; background: " + this + "; display: inline-block;'></a> ");
10+
});
11+
12+
$('#colored_sketch').sketch();
13+
});
14+
</script>
15+
</head>
16+
<body>
17+
<div id='tools'></div>
18+
<canvas id='colored_sketch' width='600' height='600' style='border: 2px solid black'></canvas>
19+
</body>
20+
</html>

src/sketch.coffee

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
(($)->
2-
$.fn.sketch = (options)->
2+
$.fn.sketch = (opts)->
33
$.error('Sketch can only be called on one element at a time.') if this.length > 1
44

5+
options = $.extend {
6+
toolLinks: true
7+
}, opts
58
el = this.get(0)
69
$el = $(el)
710
context = el.getContext '2d'
811

912
$el.data 'sketch.clicks', []
1013
$el.data 'sketch.painting', false
14+
$el.data 'sketch.color', '#000000'
1115

1216
painting = ->
1317
!!$el.data('sketch.painting')
@@ -17,14 +21,15 @@
1721
$el.data 'sketch.painting', false
1822
clicks = ->
1923
$el.data 'sketch.clicks'
24+
currentColor = ->
25+
$el.data 'sketch.color'
2026

2127
addClick = (x,y,dragging)->
22-
clicks().push {x: x, y: y, dragging: dragging}
28+
clicks().push {x: x, y: y, dragging: dragging, color: currentColor()}
2329

2430
redraw = ()->
2531
el.width = el.width
2632

27-
context.strokeStyle = '#000'
2833
context.lineJoin = 'round'
2934
context.lineWidth = 5
3035

@@ -38,10 +43,14 @@
3843

3944
context.lineTo this.x, this.y
4045
context.closePath()
46+
context.strokeStyle = this.color
4147
context.stroke()
4248

4349
previous = this
4450

51+
$el.bind 'sketch.changecolor', (e, color)->
52+
console.log "Setting color to #{color}"
53+
$el.data 'sketch.color', color
4554

4655
$el.mousedown (e)->
4756
mouseX = e.pageX - this.offsetLeft
@@ -62,5 +71,15 @@
6271
$el.mouseup stopPainting
6372
$el.mouseleave stopPainting
6473

74+
if options.toolLinks
75+
console.log "Tool links..."
76+
$('body').delegate "a[href=\"##{$el.attr('id')}\"]", 'click', (e)->
77+
console.log "Tool clicked..."
78+
$this = $(this)
79+
80+
if $this.attr('data-color')
81+
$el.trigger 'sketch.changecolor', $(this).attr('data-color')
82+
83+
false
6584
this
6685
)(jQuery)

src/sketch.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
(function() {
22
(function($) {
3-
return $.fn.sketch = function(options) {
4-
var $el, addClick, clicks, context, el, painting, redraw, startPainting, stopPainting;
3+
return $.fn.sketch = function(opts) {
4+
var $el, addClick, clicks, context, currentColor, el, options, painting, redraw, startPainting, stopPainting;
55
if (this.length > 1) {
66
$.error('Sketch can only be called on one element at a time.');
77
}
8+
options = $.extend({
9+
toolLinks: true
10+
}, opts);
811
el = this.get(0);
912
$el = $(el);
1013
context = el.getContext('2d');
1114
$el.data('sketch.clicks', []);
1215
$el.data('sketch.painting', false);
16+
$el.data('sketch.color', '#000000');
1317
painting = function() {
1418
return !!$el.data('sketch.painting');
1519
};
@@ -22,17 +26,20 @@
2226
clicks = function() {
2327
return $el.data('sketch.clicks');
2428
};
29+
currentColor = function() {
30+
return $el.data('sketch.color');
31+
};
2532
addClick = function(x, y, dragging) {
2633
return clicks().push({
2734
x: x,
2835
y: y,
29-
dragging: dragging
36+
dragging: dragging,
37+
color: currentColor()
3038
});
3139
};
3240
redraw = function() {
3341
var previous;
3442
el.width = el.width;
35-
context.strokeStyle = '#000';
3643
context.lineJoin = 'round';
3744
context.lineWidth = 5;
3845
previous = null;
@@ -45,10 +52,15 @@
4552
}
4653
context.lineTo(this.x, this.y);
4754
context.closePath();
55+
context.strokeStyle = this.color;
4856
context.stroke();
4957
return previous = this;
5058
});
5159
};
60+
$el.bind('sketch.changecolor', function(e, color) {
61+
console.log("Setting color to " + color);
62+
return $el.data('sketch.color', color);
63+
});
5264
$el.mousedown(function(e) {
5365
var mouseX, mouseY;
5466
mouseX = e.pageX - this.offsetLeft;
@@ -68,6 +80,18 @@
6880
});
6981
$el.mouseup(stopPainting);
7082
$el.mouseleave(stopPainting);
83+
if (options.toolLinks) {
84+
console.log("Tool links...");
85+
$('body').delegate("a[href=\"#" + ($el.attr('id')) + "\"]", 'click', function(e) {
86+
var $this;
87+
console.log("Tool clicked...");
88+
$this = $(this);
89+
if ($this.attr('data-color')) {
90+
$el.trigger('sketch.changecolor', $(this).attr('data-color'));
91+
}
92+
return false;
93+
});
94+
}
7195
return this;
7296
};
7397
})(jQuery);

0 commit comments

Comments
 (0)