-
Notifications
You must be signed in to change notification settings - Fork 18
/
rectangle.html
181 lines (158 loc) · 5.54 KB
/
rectangle.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
<!DOCTYPE>
<html>
<head>
<title>Primitive Paint</title>
<!-- bootstrap CDN -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/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">
var background = "#eee";
var isEraser = false;
var pathsCreated = [];
var pathsRemoved = [];
function draw(event) {
var path;
var range = Number(document.getElementById("range").value);
var BrushColor = document.getElementById("color").value;
var filledColor = document.getElementById("fillColor").value;
path = new Path.Rectangle({
x: event.point.x,
y: event.point.y,
width: range,
height: range
});
pathsCreated.push(path);
path.fillColor = filledColor;
path.strokeColor = BrushColor;
}
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;
}
function erase(event) {
var epath;
var range = Number(document.getElementById("range").value);
var BrushColor = background;
var filledColor = background;
epath = new Path.Circle({
x: event.point.x,
y: event.point.y,
radius: range
});
epath.fillColor = filledColor;
epath.strokeColor = BrushColor;
}
var paint = function (event) {
if (!isEraser) {
draw(event);
}
else {
erase(event);
}
}
tool.onMouseDown = paint;
tool.onMouseDrag = paint;
function onKeyDown(event) {
var range = Number(document.getElementById("range").value);
var key = event.character.toUpperCase();
if (key === 'Z') {
if (event.event.ctrlKey) {
if (pathsCreated.length) {
var lastPath = pathsCreated[pathsCreated.length - 1];
lastPath.remove();
pathsRemoved.push(lastPath);
pathsCreated.pop();
}
} else {
isEraser = !isEraser;
if (isEraser) {
document.querySelector("#eraser").innerText = "Pen";
}
else {
document.querySelector("#eraser").innerText = "Eraser";
}
}
}
if (key === 'Y' && event.event.ctrlKey) {
if (pathsRemoved.length) {
var lastPath = pathsRemoved[pathsRemoved.length - 1];
path = new Path.Rectangle({
x: lastPath.x,
y: lastPath.y,
width: lastPath.width,
height: lastPath.height,
fillColor: lastPath.fillColor,
strokeColor: lastPath.strokeColor
});
pathsCreated.push(path);
pathsRemoved.pop();
}
}
if (key === 'Q') {
document.getElementById("range").setAttribute("value", Math.min(range + 3, 200));
}
if (key === 'A') {
document.getElementById("range").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";
}
})
</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" href="simplePen.html">Simple Pad</a>
<a class="nav-link active" 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">Background Color : </label></strong>
<input type="color" name="" id="bgcolor" />
<strong><label for="color">Border Color : </label></strong>
<input type="color" name="" id="color" />
<strong><label for="fillColor">Fill Color : </label></strong>
<input type="color" name="" id="fillColor" value="#eeeeee" />
<strong><label for="range">Size</label></strong>
<input type="range" name="" id="range" min="1" max="200">
<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>