-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsimplePen.html
289 lines (235 loc) · 9.66 KB
/
simplePen.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<!DOCTYPE>
<html>
<head>
<title>Primitive Paint</title>
<!-- bootstrap CDN -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="assets/css/paper.css" />
<link rel="icon" href="assets/images/color-circle.png" />
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/paper.js/0.12.2/paper-full.min.js"></script>
<script type="text/paperscript" canvas="myCanvas">
// store paths created/removed for undo/redo purposes
var pathsCreated = [];
var pathsRemoved = [];
var numOfDivisions = 1;
var BrushColor = "#000000";
var background = "#eeeeee";
var isEraser = false;
var bgColor = document.getElementById("bgcolor");
bgColor.onchange = function (e){
background = e.target.value;
var rect = new Path.Rectangle({
point: [0, 0],
size: [view.size.width, view.size.height],
strokeColor: 'white',
selected: true
});
rect.sendToBack();
rect.fillColor = e.target.value;
}
// Only execute onMouseDrag when the mouse
// has moved at least 1 points:
tool.minDistance = 1;
tool.onMouseDown = function (event) {
var range = Number(document.getElementById("sizeSlider").value);
if(isEraser) range = range * 5; // Making Eraser size bigger than pen
BrushColor = (isEraser ? background : document.getElementById("color").value);
background = document.getElementById("bgcolor").value;
// create room for new path to be added
pathsCreated.push([]);
var angle = 0;
// Create a new path every time the mouse is clicked
// Create as many as there are numOfDivisions
for (var i = 0; i < numOfDivisions; i++) {
var path = new Path();
// rotate event.point by 'angle'
var rotatedPoint = rotate(event.point, angle);
path.add(rotatedPoint);
path.strokeColor = BrushColor;
path.strokeWidth = range;
path.strokeCap = 'round';
path.strokeJoin = 'round';
angle += Math.PI * 2 / numOfDivisions;
// add path to pathsCreated array
pathsCreated[pathsCreated.length - 1].push(path);
}
}
tool.onMouseDrag = function (event) {
var angle = 0;
// Add a point to the path every time the mouse is dragged
for (var i = 0; i < numOfDivisions; i++) {
var rotatedPoint = rotate(event.point, angle);
path = pathsCreated[pathsCreated.length - 1][i];
path.add(rotatedPoint);
path.strokeColor = BrushColor;
path.strokeCap = 'round';
path.strokeJoin = 'round';
angle += Math.PI * 2 / numOfDivisions;
}
}
function onKeyDown(event) {
var key = event.character.toUpperCase();
if (key === 'Z') {
// Undo if ctrl was pressed else toggle eraser
if (event.event.ctrlKey) {
// proceed only if pathsCreated is not empty
if (pathsCreated.length) {
// add room for removed paths to be added in pathsRemoved array
pathsRemoved.push([]);
// remove last path in pathsCreated array
for (var i = 0; i < numOfDivisions; i++) {
var lastPath = pathsCreated[pathsCreated.length - 1][i];
lastPath.remove();
// update pathsCreated
pathsRemoved[pathsRemoved.length - 1].push(lastPath);
}
// update pathsRemoved
pathsCreated.pop();
}
} else {
isEraser = !isEraser;
if (isEraser) {
document.querySelector("#eraser").innerText = "Pen";
}
else {
document.querySelector("#eraser").innerText = "Eraser";
}
}
}
// Redo
if (key === 'Y' && event.event.ctrlKey) {
// proceed only if pathsRemoved is not empty
if (pathsRemoved.length) {
// add room for restored paths to be added in pathsCreated array
pathsCreated.push([]);
// add last path from pathsRemoved array
for (var i = 0; i < numOfDivisions; i++) {
var lastPath = pathsRemoved[pathsRemoved.length - 1][i];
var path = new Path({
segments: lastPath.segments,
strokeColor: lastPath.strokeColor,
strokeWidth: lastPath.strokeWidth,
strokeCap: lastPath.strokeCap,
strokeJoin: lastPath.strokeJoin,
});
// update pathsCreated
pathsCreated[pathsCreated.length - 1].push(path);
}
// update pathsRemoved
pathsRemoved.pop();
}
}
if (key === 'Q') {
document.getElementById("sizeSlider").setAttribute("value", Math.min(range + 3, 100));
}
if (key === 'A') {
document.getElementById("sizeSlider").setAttribute("value", Math.max(range - 3, 1));
}
}
document.querySelector("#clear").addEventListener("click", function () {
location.reload();
})
document.querySelector("#eraser").addEventListener("click", function (event) {
isEraser = !isEraser;
if (isEraser) {
event.target.innerText = "Pen";
}
else {
event.target.innerText = "Eraser";
}
})
// returns a point rotated by given angle wrt centre of the canvas
function rotate(point, angle) {
var rotatedPoint = {x: undefined, y: undefined};
// convert screen coordinates to cartesian coordinates
rotatedPoint.x = point.x - innerWidth / 2;
rotatedPoint.y = -(point.y - innerHeight / 2);
// apply rotation
var rotatedX = rotatedPoint.x * Math.cos(angle) - rotatedPoint.y * Math.sin(angle);
var rotatedY = rotatedPoint.x * Math.sin(angle) + rotatedPoint.y * Math.cos(angle);
// transform points back to screen coordinates
rotatedPoint.x = rotatedX + innerWidth / 2;
rotatedPoint.y = -rotatedY + innerHeight / 2;
return rotatedPoint;
}
// update numOfDivisions when slider value is changed
var slider = document.querySelector("#divisionsSlider");
slider.addEventListener("change", function () {
document.querySelector("#divisionsLabel").innerText = "No. of divisions: " + slider.value;
numOfDivisions = slider.value;
drawDivisionLines();
})
slider.addEventListener("mousemove", function () {
document.querySelector("#divisionsLabel").innerText = "No. of divisions: " + slider.value;
numOfDivisions = slider.value;
drawDivisionLines();
})
var diagonalSize = Math.sqrt(Math.pow(innerWidth / 2, 2) + Math.pow(innerHeight / 2, 2));
var divisionLines;
function drawDivisionLines () {
// clear previous divisionLines if they are already drawn
clearDivisionLines();
// don't draw if numOfDivisions == 1 or showDivisionLines is unchecked
if (numOfDivisions == 1 || !showDivisionLines.checked) return;
divisionLines = new Path();
divisionLines.strokeColor = BrushColor;
var angle = 0;
for (var i = 0; i < numOfDivisions; i++) {
divisionLines.add(innerWidth / 2, innerHeight / 2);
var point = rotate({x: innerWidth / 2, y: -diagonalSize}, angle);
divisionLines.add(point);
angle += Math.PI * 2 / numOfDivisions;
}
}
function clearDivisionLines () {
if (divisionLines) divisionLines.remove();
}
var showDivisionLines = document.querySelector("#showDivisionLines");
showDivisionLines.addEventListener("change", function () {
if (showDivisionLines.checked) {
drawDivisionLines();
} else {
clearDivisionLines();
}
})
</script>
</script>
<div>
<script>
function createDownload() {
const downloadURL = document.getElementById('myCanvas').toDataURL();
document.getElementById('downloadLink').href = downloadURL;
}
</script>
</head>
<body style="cursor: pointer">
<nav id="navbar">
<a class="nav-link" href="index.html">Home</a>
<a class="nav-link active" href="simplePen.html">Simple Pad</a>
<a class="nav-link" href="rectangle.html">Rectangle Pad</a>
<a class="nav-link" href="circle.html">Circle Pad</a>
<a class="nav-link" href="ellipse.html">Ellipse Pad</a>
</nav>
<div style="padding: 10px 20px">
<strong><label for="color">Select Color : </label></strong>
<input type="color" name="" id="color" />
<strong><label for="color">Background Color : </label></strong>
<input type="color" name="" id="bgcolor" value="#eeeeee"/>
<strong><label for="sizeSlider">Size</label></strong>
<input type="range" name="" id="sizeSlider" min="1" max="100" value="4">
<strong><label for="divisionsSlider" id="divisionsLabel">No. of divisions: 1</label></strong>
<input type="range" id="divisionsSlider" min="1" max="100" value="1">
<input type="checkbox" id="showDivisionLines" checked>
<strong><label for="showDivisionLines">Show division Lines</label></strong>
<a id='downloadLink' download="canvas.jpg" onclick="createDownload()" class="btn btn-secondary" style="float: right" > Save </a>
<button id="clear" class="btn btn-secondary" style="float: right">Clear Screen</button>
<button id="eraser" class="btn btn-secondary" style="float: right">Eraser</button>
</div>
<canvas id="myCanvas" resize></canvas>
</body>
</html>