-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper-canvas.html
108 lines (95 loc) · 2.47 KB
/
paper-canvas.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<polymer-element name="paper-canvas" attributes="brush background">
<template>
<canvas id="canvas"></canvas>
<style>
:host {
display: block;
width: 100%;
height: 100%;
}
#canvas {
width: 100%;
height: 100%;
float:left;
cursor: crosshair;
//background: {{background}};
}
</style>
</template>
<script>
Polymer('paper-canvas', {
background: 'white',
brush:{
name : 'Default Brush',
description : 'Default Brush',
path : null,
initialize: function(){
this.path = new paper.Path();
this.path.strokeColor = 'black';
this.path.opacity = 1;
return this.path;
},
paint : function(point){
if (point) {
this.path.add(point);
}
return this.path;
}
},
clear: function(){},
import: function(){
},
export: function(){
return this.$.canvas.toDataURL("image/png");
},
prepare: function() {
var self = this;
var canvas = this.$.canvas;
paper.setup(canvas);
var background = new paper.Path.Rectangle(paper.view.bounds);
background.fillColor = 'white';
var tool = new paper.Tool();
var path;
var paths = [];
this.clear = function(){
while(paths.length>0){
var path = paths.pop();
path.remove();
}
}
// Define a mousedown and mousedrag handler
tool.onMouseDown = function(ev) {
if(ev.event.altKey) return;
path = self.brush.initialize();
self.brush.paint(ev.point);
paths.push(path);
};
function undo() {
if (paths.length === 0) {
return;
}
var path = paths.pop();
path.remove();
paper.view.draw();
}
$(document).on('keydown', function(ev) {
if (ev.ctrlKey && ev.keyCode === 90) {
undo();
}
});
tool.onMouseUp = function(ev) {
if(ev.event.altKey) return;
if (ev.point){
self.brush.paint(ev.point);
}
}
tool.onMouseDrag = function(ev) {
if(ev.event.altKey) return;
if (ev.point){
self.brush.paint(ev.point);
}
};
}
});
</script>
</polymer-element>