|
// Generated by CoffeeScript 1.4.0 |
|
(function() { |
|
var SWATCH_D, stroke_width, stroke_opacity, active_color, active_stroke_opacity, active_line, active_stroke_width, canvas, drag, drawing_data, lines_layer, palette, redraw, render_line, swatches, trash_btn, ui, pen_btn, paint_btn; |
|
|
|
SWATCH_D = 25; |
|
|
|
render_line = d3.svg.line().x(function(d) { |
|
return d[0]; |
|
}).y(function(d) { |
|
return d[1]; |
|
}).interpolate('basis'); |
|
|
|
drawing_data = { |
|
lines: [ |
|
{ |
|
color: "#1b9e77", |
|
points: [[451, 448], [447, 447]] |
|
} |
|
] |
|
}; |
|
|
|
active_line = null; |
|
|
|
active_color = "#333333"; |
|
|
|
active_stroke_width = "2px"; |
|
|
|
active_stroke_opacity = "0.2"; |
|
|
|
canvas = d3.select('#canvas'); |
|
|
|
lines_layer = canvas.append('g'); |
|
|
|
ui = d3.select('#ui'); |
|
|
|
palette = ui.append('g').attr({ |
|
transform: "translate(" + (2 + SWATCH_D / 2) + "," + (2 + SWATCH_D / 2) + "," + (2 + SWATCH_D / 2) + "," + (2 + SWATCH_D / 2) + ")", |
|
transform: "translate(800,430)" |
|
}); |
|
|
|
swatches = palette.selectAll('swatch').data(["#333333", "#ffffff", "#1b9e77", "#d95f02", "#7570b3", "#e7298a", "#66a61e", "#e6ab02", "#a6761d", "#666666"]); |
|
|
|
trash_btn = ui.append('text').html('').attr({ |
|
"class": 'btn', |
|
dy: '0.35em', |
|
transform: 'translate(940,20)' |
|
}).on('click', function() { |
|
drawing_data.lines = []; |
|
return redraw(); |
|
}); |
|
|
|
pen_btn = ui.append('text').html('').attr({ |
|
"class": 'btn', |
|
dy: '0.35em', |
|
transform: 'translate(940,80)' |
|
}).on('click', function(e) { |
|
active_stroke_width = "2px"; |
|
active_stroke_opacity = "0.9"; |
|
active_line.classed('active', false); |
|
return d3.select(this).classed('active', true); |
|
}); |
|
|
|
paint_btn = ui.append('text').html('').attr({ |
|
"class": 'btn', |
|
dy: '0.35em', |
|
transform: 'translate(940,140)' |
|
}).on('click', function(f) { |
|
active_stroke_width = "10px"; |
|
active_stroke_opacity = "0.2"; |
|
active_line.classed('active', false); |
|
return d3.select(this).classed('active', true); |
|
}); |
|
|
|
swatches.enter().append('circle').attr({ |
|
"class": 'swatch', |
|
cx: function(d, i) { |
|
return i * (SWATCH_D + 4) / 2; |
|
}, |
|
cy: function(d, i) { |
|
if (i % 2) { |
|
return SWATCH_D; |
|
} else { |
|
return 0; |
|
} |
|
}, |
|
r: SWATCH_D / 2, |
|
fill: function(d) { |
|
return d; |
|
} |
|
}).on('click', function(d) { |
|
active_color = d; |
|
swatches.classed('active', false); |
|
return d3.select(this).classed('active', true); |
|
}); |
|
|
|
swatches.each(function(d) { |
|
if (d === active_color) { |
|
return d3.select(this).classed('active', true); |
|
} |
|
}); |
|
|
|
drag = d3.behavior.drag(); |
|
|
|
drag.on('dragstart', function() { |
|
active_line = { |
|
points: [], |
|
color: active_color, |
|
stroke_width : active_stroke_width |
|
}; |
|
drawing_data.lines.push(active_line); |
|
return redraw(active_line); |
|
}); |
|
|
|
drag.on('drag', function() { |
|
active_line.points.push(d3.mouse(this)); |
|
return redraw(active_line); |
|
}); |
|
|
|
drag.on('dragend', function() { |
|
if (active_line.points.length === 0) { |
|
drawing_data.lines.pop(); |
|
} |
|
active_line = null; |
|
return console.log(drawing_data); |
|
}); |
|
|
|
canvas.call(drag); |
|
|
|
redraw = function(specific_line) { |
|
var lines; |
|
lines = lines_layer.selectAll('.line').data(drawing_data.lines); |
|
lines.enter().append('path').attr({ |
|
"class": 'line', |
|
stroke: function(d) { |
|
return d.color; |
|
} |
|
}).each(function(d) { |
|
return d.elem = d3.select(this); |
|
}); |
|
if (specific_line != null) { |
|
specific_line.elem.attr({ |
|
d: function(d) { |
|
return render_line(d.points); |
|
} |
|
}); |
|
} else { |
|
lines.attr({ |
|
d: function(d) { |
|
return render_line(d.points); |
|
} |
|
}); |
|
} |
|
return lines.exit().remove(); |
|
}; |
|
|
|
redraw(); |
|
|
|
}).call(this); |