-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.html
492 lines (402 loc) · 13.9 KB
/
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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html" ; charset="UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- External CSS Document(s) -->
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<link href="https://fonts.googleapis.com/css2?family=Yusei+Magic&display=swap" rel="stylesheet">
<title>StarDraw: Your Self-Care Art Journal</title>
<!-- Eternal JavaScript Document(s) -->
<script type="text/javascript" src="js/canvas.js"></script>
<style>
@charset "UTF-8";
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Yusei Magic', sans-serif;
color: #767676;
letter-spacing: 0.05em;
line-height: 2em;
text-transform: lowercase;
}
.section {
padding-top: 20px;
padding-bottom: 20px;
margin: 0 auto;
width: 80%;
max-width: 700px;
text-align: center;
}
body {
background-color: #f0ceec;
}
.button {
font-family: inherit;
display: inline;
padding: 5px 12px;
cursor: pointer;
text-align: center;
text-decoration: none;
outline: none;
color: grey;
background-color: white;
border: none;
border-radius: 15px;
box-shadow: 0 3px #999;
}
.button:hover {
background-color: #f0ebee;
}
.button:active {
background-color: #c8c8c8;
box-shadow: 0 5px #666;
transform: translateY(3px);
}
h1 {
color: white;
text-align: center;
margin-bottom: 10px;
font-size: 26px;
}
textarea {
width: 80%;
height: 300px;
resize: none;
border-radius: 16px;
border: 0px;
padding: 30px 30px;
font-family: inherit;
font-size: 14px;
text-transform: none;
}
textarea:focus {
outline: none;
}
#quote {
text-align: center;
margin: 0 auto;
padding: 5%;
font-size: 16px;
background-color: white;
border-radius: 16px;
width: 80%;
}
::placeholder {
opacity: 0.6;
}
#wrapper {
position: relative;
margin: 0 auto;
max-width: 900px;
}
/* placeholder for canvas element */
#canvasPlaceholder {
width: 700px;
height: 600px;
border-radius: 16px;
margin: auto;
cursor: crosshair;
}
#buttons {
position: absolute;
z-index: 2;
line-height: 0;
vertical-align: middle;
display: flex;
flex-direction: column;
width: 10%;
max-width: 100px;
margin: 0 auto;
margin-top: 10px;
}
.canvasButton {
cursor: pointer;
opacity: 0.6;
text-decoration: none;
outline: none;
color: none;
background-color: Transparent;
border: none;
margin: 20%;
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
.canvasButton:hover {
opacity: 0.4;
}
.canvasButton:active {
opacity: 0.8;
}
::selection {
background: #fbf0fb;
}
::-moz-selection {
}
/* drawing canvas element visible to user */
#drawingCanvas {
width: 700px;
height: 600px;
border-radius: 16px;
position: absolute;
overflow: hidden;
background-color: transparent;
}
/* temporary canvas being updated every time user draws */
#tempCanvas {
position: absolute;
width: 700px;
height: 600px;
border-radius: 16px;
overflow: hidden;
}
.noscript {
font-size: 14px;
}
</style>
</head>
<body>
<!-- random prompts given to the user. a new one can be requested by pressing the button-->
<div class="section">
<h1>💭 prompt</h1>
<div id="prompt"></div>
</div>
<div class="section">
<button class="button" id="getPrompt" button onclick="getPrompt()">
get new prompt</button>
</div>
<script>
function getPrompt() {
var prompts = [];
prompts.push("How are you feeling today?");
prompts.push("What was the last dream you remember?");
prompts.push("What's something you're looking forward to?");
prompts.push("What was the last dream you remember?");
prompts.push("Draw a nostalgic moment from your childhood.");
prompts.push("What did you wish for when you were younger?");
prompts.push("If you had a magic wand and could make anything you wanted appear, how would you change your life?");
prompts.push("Who is someone/something that’s always motivated you?");
prompts.push("Do you feel emotionally carefree or burdened? Draw something light or heavy that represents how you feel.");
prompts.push("Do you have something emotionally significant on or near you? Draw the item or a memory you associate with it.");
prompts.push("Redraw something from earlier this week. Do you remember how you felt at the time? Interpret those feelings and draw the same prompt, but with how you feel now in mind.");
prompts.push("If you were any animal, what would you be? What does that animal represent to you?");
prompts.push("If you had to choose a fictional world to live in, where would it be? Consider aspects of that world you enjoy.");
prompts.push("Draw circles along with your breath. Start when you breathe in, and breathe out when the circle is half done. Finish exhaling and complete the circle.");
prompts.push("Find something nearby. Observe its color, curves, edges, and lines. Pay attention to the present as you feel sensations of your hand across the paper or screen. Recognize emotions you get about the object as you draw it, but let them pass through your thoughts.");
prompts.push("Without lifting your pencil, let yourself fill with positive emotions. What are you feeling? Peace? Serenity? Acceptance?");
var prompt = prompts[Math.floor(Math.random() * prompts.length)];
document.getElementById("prompt")
.innerHTML = '<p>' + prompt + '</p>';
}
</script>
<!-- canvas heading -->
<h1>🎨 canvas</h1>
<!-- canvas features, such as draw, eraser, increase pen stroke, and clear canvas -->
<div id="wrapper">
<div id="buttons">
<button class="canvasButton" id="pen">
<img style="width: 100%" src="https://cdn.discordapp.com/attachments/802400122889699328/802813463381934120/paint-brush.png" onclick="tool = pen; context.strokeStyle = '#514d44';" />
</button>
<button class="canvasButton" id="eraser">
<img style="width: 100%" src="https://cdn.discordapp.com/attachments/802400122889699328/802813695267831848/eraser.png" onclick="context.strokeStyle = '#FFF';" />
</button>
<button class="canvasButton">
<img style="width: 100%" src="https://cdn.discordapp.com/attachments/802400122889699328/802783792820387870/smol_circle.png" onclick="context.lineWidth--;" />
</button>
<button class="canvasButton">
<img style="width: 100%" src="https://cdn.discordapp.com/attachments/802400122889699328/802783752602386452/circle.png" onclick="context.lineWidth++;" />
</button>
<button class="canvasButton" id="clear_canvas" button onclick="clear_canvas()">
<img style="width: 100%" src="https://img.icons8.com/wired/512/000000/trash.png" style='transform:translate(0px, 3px)' />
</button>
</div>
<div id="canvasPlaceholder">
<!-- text after noscript will appear if user is not using a browser compatible with html canvas -->
<canvas id="drawingCanvas" height="600px" width="700px">
<p class="noscript">We're sorry, this web application is currently not supported with your browser. Please use an alternate browser or download a supported <br />browser. Supported browsers: <a href="http://www.google.com/chrome">Google Chrome</a>, <a href="http://www.opera.com">Opera</a>, <a href="http://www.mozilla.com">Firefox</a>, <a href="http://www.apple.com/safari">Safari</a>, <br />and <a href="http://www.konqueror.org">Konqueror</a>. Also make sure your JavaScript is enabled.</p>
</canvas>
</div>
<!-- interactive button for saving canvas -->
<div class="section">
<button class="button" id="save canvas" button onclick="save_img()">
save canvas</button>
</div>
</div>
<!-- journal element -->
<div class="section">
<h1>📃 Journal</h1>
<textarea id="journal" textarea name="journal" placeholder="type away~" spellcheck="false"></textarea>
<div class="section">
<!-- interactive button for saving journal -->
<button class="button" id="save journal" button onclick="save_journal()">
save journal</button>
</div>
</div>
<!-- script file for drawing canvas, saving canvas, and saving journal -->
<script>
var context;
// if window can add event listener, then load an event listener and initialize canvas
if (window.addEventListener) {
window.addEventListener('load', function () {
var canvas, canvaso, contexto;
var tool;
var tool_default = 'pen';
// initializes canvas
function init() {
canvaso = document.getElementById('drawingCanvas');
if (!canvaso) {
alert('Error! Canvas element not found.');
return;
}
if (!canvaso.getContext) {
alert('Error! No canvas.getContext!');
return;
}
contexto = canvaso.getContext('2d');
if (!contexto) {
alert('Error! Failed to getContext!');
return;
}
var container = canvaso.parentNode;
canvas = document.createElement('canvas');
if (!canvas) {
alert('Error! Cannot create a new canvas element!');
return;
}
canvas.id = 'tempCanvas';
canvas.width = canvaso.width;
canvas.height = canvaso.height;
container.appendChild(canvas);
context = canvas.getContext('2d');
// set defaults
context.strokeStyle = "#767676"
context.lineWidth = 5.0;
// set canvas
context.fillStyle = "#FFF";
context.fillRect(0, 0, canvas.width, canvas.height);
// set default tool
var tool_select = document.getElementById('pen');
if (!tool_select) {
alert('Error! Failed to get the select element!');
return;
}
// add an event listener to the current tool selected
tool_select.addEventListener('change', ev_tool_change, false);
// if the default tool isn't null, then set current tool to default(?)
if (tools[tool_default]) {
tool = new tools[tool_default]();
tool_select.value = tool_default;
}
// event listeners for pressing down on mouse, moving mouse, and pressing off of mouse
canvas.addEventListener('mousedown', ev_canvas, false);
canvas.addEventListener('mousemove', ev_canvas, false);
canvas.addEventListener('mouseup', ev_canvas, false);
// initializes a prompt on load
getPrompt();
}
function ev_canvas(ev) {
if (ev.layerX || ev.layerX == 0) { // Firefox
ev._x = ev.layerX;
ev._y = ev.layerY;
} else if (ev.offsetX || ev.offsetX == 0) { // Opera
ev._x = ev.offsetX;
ev._y = ev.offsetY;
}
// get the tool's event handler.
var func = tool[ev.type];
if (func) {
func(ev);
}
}
// changes current tool (not necessary for current implementation)
function ev_tool_change(ev) {
if (tools[this.value]) {
tool = new tools[this.value]();
}
}
// create the temporary canvas on top of the canvas, which is cleared each time the user draws so a new canvas can be drawn
function img_update() {
contexto.drawImage(canvas, 0, 0);
context.clearRect(0, 0, canvas.width, canvas.height);
}
// list of tools
var tools = {};
// pen tool's functionality
tools.pen = function () {
var tool = this;
this.started = false;
// registers mouse has been clicked and will start to move
this.mousedown = function (ev) {
context.beginPath();
context.moveTo(ev._x, ev._y);
tool.started = true;
};
// draws with pen tool while mouse is moving, if mouse has been clicked
this.mousemove = function (ev) {
if (tool.started) {
context.lineTo(ev._x, ev._y);
context.stroke();
}
};
// registers mouse is no longer being pressed, and will stop
this.mouseup = function (ev) {
if (tool.started) {
tool.mousemove(ev);
tool.started = false;
img_update();
}
};
};
init();
}, false);
}
// saves canvas to a png file
function save_img() {
// gets canvas element and converts it into a dataURL
var canvas = document.getElementById('drawingCanvas');
var data = canvas.toDataURL('image/png');
// creates an element of type link
var a = document.createElement('a');
// sets its link to the converted canvas data
a.href = data;
// sets file name to be downloaded
a.download = 'emotions.png';
// executes link
a.click();
}
// clears canvas
function clear_canvas() {
// gets canvas element
var canvas = document.getElementById('drawingCanvas');
// gets contents of canvas
var context = canvas.getContext('2d');
// sets fill style to be white
context.fillStyle = "#FFF";
// fills the canvas
context.fillRect(0, 0, canvas.width, canvas.height);
}
// saves journal entry to a txt file
function save_journal() {
// gets textarea's value
var text = document.getElementById("journal")
.value;
// retains all line breaks to keep journal consistent
text = text.replace(/\n/g, "\r\n");
// TODO: comment
var blob = new Blob([text], { type: "text/plain" });
var anchor = document.createElement("a");
anchor.download = "journal-entry.txt";
anchor.href = window.URL.createObjectURL(blob);
anchor.target = "_blank";
anchor.style.display = "none"; // just to be safe!
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
}
</script>
</body>
</html>