Skip to content

Commit c0ea192

Browse files
author
Michael Bleigh
committed
Adds size changing tool links.
1 parent 38001bb commit c0ea192

File tree

3 files changed

+70
-47
lines changed

3 files changed

+70
-47
lines changed

index.html

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<script src="http://code.jquery.com/jquery-latest.js"></script>
77
<script src='src/sketch.js'></script>
88
<style type='text/css'>
9-
body { font-family: "Open Sans", sans-serif; }
9+
body { font-family: "Open Sans", sans-serif; color: #444; }
1010
h1, h2, h3, h4 { font-family: Yellowtail; font-weight: normal; color: #06a; }
1111

1212
#wrapper { width: 800px; margin: 0 auto; }
@@ -21,6 +21,18 @@
2121
height: 300px;
2222
border: 1px solid #ccc;
2323
}
24+
25+
pre.source {
26+
background: #e5eeee;
27+
padding: 10px 20px;
28+
width: 760px;
29+
overflow-x: auto;
30+
border: 1px solid #acc;
31+
}
32+
33+
#colors_tools a {
34+
border: 1px solid black; width: 30px; height: 30px; line-height: 30px; vertical-align: middle; text-align: center; text-decoration: none; display: inline-block; color: black; font-weight: bold;
35+
}
2436
</style>
2537
</head>
2638
<body>
@@ -37,17 +49,17 @@ <h1>Simple Example</h1>
3749
<p>Click and drag in the box below to draw a simple black line.</p>
3850

3951
<div class='demo'>
40-
<canvas id='simple_sketch' width='800' height='300'></canvas>
41-
<script type='text/javascript'>
42-
$(function() {
43-
$('#simple_sketch').sketch();
44-
});
45-
</script>
52+
<canvas id='simple_sketch' width='800' height='300'></canvas>
53+
<script type='text/javascript'>
54+
$(function() {
55+
$('#simple_sketch').sketch();
56+
});
57+
</script>
4658
</div>
4759
</article>
4860

4961
<article class='example'>
50-
<h1>Color Change</h1>
62+
<h1>Changing Color/Size</h1>
5163

5264
<p></p>
5365

@@ -56,8 +68,11 @@ <h1>Color Change</h1>
5668
<canvas id='colors_sketch' width='800' height='300'></canvas>
5769
<script type='text/javascript'>
5870
$(function() {
59-
$.each(['#f00', '#ff0', '#0f0', '#0ff', '#00f', '#000', '#fff'], function() {
60-
$('#colors_tools').append("<a href='#colors_sketch' data-color='" + this + "' style='border: 1px solid black; width: 30px; height: 30px; background: " + this + "; display: inline-block;'></a> ");
71+
$.each(['#f00', '#ff0', '#0f0', '#0ff', '#00f', '#000', '#fff'], function() {
72+
$('#colors_tools').append("<a href='#colors_sketch' data-color='" + this + "' style='background: " + this + ";'></a> ");
73+
});
74+
$.each([3, 5, 10, 15], function() {
75+
$('#colors_tools').append("<a href='#colors_sketch' data-size='" + this + "' style='background: #ccc'>" + this + "</a> ");
6176
});
6277
$('#colors_sketch').sketch();
6378
});
@@ -75,7 +90,8 @@ <h1>Color Change</h1>
7590

7691
$('article.example').each(function() {
7792
$(this).append("<pre class='source'></pre>");
78-
$(this).find("pre.source").html(escapeHTML($(this).find("div.demo").html()));
93+
var html = $(this).find("div.demo").html().replace(/ /g,"");
94+
$(this).find("pre.source").html(escapeHTML(html));
7995
});
8096
</script>
8197
</body>

src/sketch.coffee

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@
2020
@actions = []
2121
@action = []
2222

23+
@canvas.bind 'sketch.changecolor', (e, color)->
24+
$(this).data('sketch').color = color
25+
@canvas.bind 'sketch.changesize', (e, size)->
26+
$(this).data('sketch').size = size
27+
@canvas.bind 'mousedown mouseup mousemove mouseleave touchstart touchmove touchend touchcancel', @onEvent
28+
29+
if @options.toolLinks
30+
$('body').delegate "a[href=\"##{@canvas.attr('id')}\"]", 'click', (e)->
31+
$this = $(this)
32+
$canvas = $($this.attr('href'))
33+
if $this.attr('data-color')
34+
$canvas.trigger 'sketch.changecolor', $(this).attr('data-color')
35+
if $this.attr('data-size')
36+
$canvas.trigger 'sketch.changesize', parseFloat($(this).attr('data-size'))
37+
false
38+
39+
2340
startPainting: ->
2441
@painting = true
2542
@action = {
@@ -40,9 +57,9 @@
4057

4158
addEvent: (e)->
4259
switch e.type
43-
when 'mousedown'
60+
when 'mousedown', 'touchstart'
4461
@startPainting()
45-
when 'mouseup', 'mouseout'
62+
when 'mouseup', 'mouseout', 'touchend', 'touchcancel'
4663
@stopPainting()
4764

4865
if @painting
@@ -82,23 +99,7 @@
8299

83100
$.fn.sketch = (opts)->
84101
$.error('Sketch can only be called on one element at a time.') if this.length > 1
85-
86102
this.data('sketch', new Sketch(this.get(0), opts))
87-
sketch = this.data('sketch')
88-
89-
this.bind 'sketch.changecolor', (e, color)->
90-
sketch.color = color
91-
this.bind 'mousedown mouseup mousemove mouseleave', sketch.onEvent
92-
93-
that = this
94-
if sketch.options.toolLinks
95-
console.log that
96-
$('body').delegate "a[href=\"##{that.attr('id')}\"]", 'click', (e)->
97-
$this = $(this)
98-
if $this.attr('data-color')
99-
that.trigger 'sketch.changecolor', $(this).attr('data-color')
100-
false
101-
102103
this
103104

104105
)(jQuery)

src/sketch.js

Lines changed: 24 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)