1
1
<template >
2
- <body style =" background-image : url (' https://png.pngtree.com/thumb_back/fh260/background/20190331/pngtree-vector-white-abstract-background-design-templates-collection-wit-image_94438.jpg' ); background-repeat : repeat ; padding-top : 48px ; padding-bottom : 25px ;" >
2
+ <body style =" padding-top : 48px ; padding-bottom : 25px ; " >
3
+ <div >
4
+ <h1 style =" font-family : georgia ; font-size : 45px ; text-align : center ;" >
5
+ <b >Create a New Badge!</b >
6
+ </h1 >
7
+ <h2
8
+ style =" margin-bottom : 30px ; font-family : georgia ; font-size : 25px ; text-align : center ;"
9
+ >
10
+ <b >Design your own badge for your organization.</b >
11
+ </h2 >
3
12
<div >
4
- <h1 style =" font-family : georgia ; font-size : 45px ; text-align : center ;" >
5
- <b >Create a New Badge!</b >
6
- </h1 >
7
- <h2 style =" margin-bottom : 30px ; font-family : georgia ; font-size : 25px ; text-align : center ;" >
8
- <b >Design your own badge for your organization.</b >
9
- </h2 >
10
- <div >
11
- <DrawButtons />
12
- <canvas id = " canvas" width =" 900" height =" 600" >
13
- </canvas >
14
- </div >
15
- <ToolButtons />
13
+ <DrawButtons />
14
+ <canvas id =" canvas" width =" 900" height =" 600" ></canvas >
16
15
</div >
17
- </body >
16
+ </div >
17
+ <ToolButtons />
18
+ </body >
18
19
</template >
19
20
20
21
<style >
21
-
22
- canvas {
23
- background-color : white ;
22
+ canvas {
23
+ background-color : #ffffff ;
24
24
border : 2px solid gray ;
25
25
border-radius : 2px ;
26
26
margin-bottom : 10px ;
27
- }
28
-
27
+ }
29
28
29
+ body {
30
+ background-image : url (" https://png.pngtree.com/thumb_back/fh260/background/20190331/pngtree-vector-white-abstract-background-design-templates-collection-wit-image_94438.jpg" );
31
+ background-repeat : repeat ;
32
+ }
30
33
</style >
31
34
32
35
<script >
33
- import ToolButtons from ' ../components/newBadge/ToolButtons' ;
34
- import DrawButtons from ' ../components/newBadge/DrawButtons' ;
36
+ import ToolButtons from " ../components/newBadge/ToolButtons" ;
37
+ import DrawButtons from " ../components/newBadge/DrawButtons" ;
35
38
36
- export default {
37
- name: ' NewBadge' ,
39
+ export default {
40
+ name: " NewBadge" ,
38
41
components: {
39
- DrawButtons,
40
- ToolButtons,
41
- }
42
+ DrawButtons,
43
+ ToolButtons
42
44
}
45
+ };
43
46
44
-
45
-
46
- window .addEventListener (" load" , () => {
47
+ window .addEventListener (" load" , () => {
47
48
const canvas = document .getElementById (" canvas" );
48
49
50
+ canvas .backgroundColor = " #ffffff" ;
51
+ let haveColor = true ;
52
+ let pencilColor;
53
+
49
54
const ctx = canvas .getContext (" 2d" );
50
55
51
56
function getMousePos (canvas , event ) {
52
- var rect = canvas .getBoundingClientRect ();
53
- return {
54
- x: (event .clientX - rect .left ) / (rect .right - rect .left ) * canvas .width ,
55
- y: (event .clientY - rect .top ) / (rect .bottom - rect .top ) * canvas .height
56
- };
57
+ var rect = canvas .getBoundingClientRect ();
58
+ return {
59
+ x:
60
+ ((event .clientX - rect .left ) / (rect .right - rect .left )) * canvas .width ,
61
+ y: ((event .clientY - rect .top ) / (rect .bottom - rect .top )) * canvas .height
62
+ };
57
63
}
58
64
59
65
ctx .strokeStyle = " #000000" ;
62
68
63
69
let shouldPaint = false ;
64
70
71
+ var selector = document .getElementById (" selector" );
72
+ var pencil = document .getElementById (" pencil" );
73
+ var eraser = document .getElementById (" eraser" );
74
+ var square = document .getElementById (" square" );
75
+ var circle = document .getElementById (" circle" );
76
+ var magic = document .getElementById (" magic" );
77
+ var text = document .getElementById (" text" );
78
+
65
79
function startDrawing () {
66
- shouldPaint = true ;
67
- var mousePos = getMousePos (canvas, event );
68
- ctx .moveTo (mousePos .x , mousePos .y );
69
- ctx .beginPath ()
70
- continueDrawing (event );
80
+ shouldPaint = true ;
81
+ var mousePos = getMousePos (canvas, event );
82
+ ctx .moveTo (mousePos .x , mousePos .y );
83
+ ctx .beginPath ();
84
+ continueDrawing (event );
71
85
}
72
86
73
87
function continueDrawing (event ) {
74
- if (shouldPaint) {
75
- var mousePos = getMousePos (canvas, event );
76
- ctx .lineTo (mousePos .x , mousePos .y )
77
- ctx .stroke ();
78
- }
88
+ if (shouldPaint) {
89
+ var mousePos = getMousePos (canvas, event );
90
+ ctx .lineTo (mousePos .x , mousePos .y );
91
+ ctx .stroke ();
92
+ }
79
93
}
80
94
81
95
function endDrawing () {
82
- shouldPaint = false ;
96
+ shouldPaint = false ;
83
97
}
84
98
85
99
// Allows user to draw on canvas
86
100
drawing ();
87
101
88
- function drawing (event ) {
89
- canvas .addEventListener (" mousedown" , startDrawing);
90
- document .addEventListener (" mouseup" , endDrawing);
91
- canvas .addEventListener (" mousemove" , continueDrawing);
102
+ function drawing () {
103
+ haveColor = true ;
104
+ if (ctx .strokeStyle == canvas .backgroundColor ) {
105
+ ctx .strokeStyle = pencilColor;
106
+ if (! pencilColor) {
107
+ ctx .strokeStyle = " #000000" ;
108
+ }
109
+ }
110
+ canvas .addEventListener (" mousedown" , startDrawing);
111
+ document .addEventListener (" mouseup" , endDrawing);
112
+ canvas .addEventListener (" mousemove" , continueDrawing);
92
113
}
93
114
94
115
// Changing colors
95
116
document .querySelectorAll (" .color" ).forEach (link => {
96
- link .addEventListener (" click" , function (event ) {
97
- ctx .strokeStyle = this .style .backgroundColor ;
98
- })
99
- })
117
+ link .addEventListener (" click" , function () {
118
+ if (haveColor) {
119
+ ctx .strokeStyle = this .style .backgroundColor ;
120
+ pencilColor = this .style .backgroundColor ;
121
+ }
122
+ });
123
+ });
100
124
101
125
// Changing brush size
102
126
document .querySelectorAll (" .size" ).forEach (link => {
103
- link .addEventListener (" click" , function (event ) {
104
- ctx .lineWidth = this .id ;
105
- })
106
- })
107
-
108
-
127
+ link .addEventListener (" click" , function () {
128
+ ctx .lineWidth = this .id ;
129
+ });
130
+ });
109
131
110
132
// Activates and deactivates buttons
111
- function removeDraw (event ) {
112
- canvas .removeEventListener (" mousedown" , startDrawing);
113
- console .log (" working" );
133
+ function removeDraw () {
134
+ canvas .removeEventListener (" mousedown" , startDrawing);
114
135
}
115
136
116
137
selector .addEventListener (" click" , removeDraw);
117
138
pencil .addEventListener (" click" , drawing);
118
- eraser .addEventListener (" click" , removeDraw);
139
+
140
+ eraser .addEventListener (" click" , function () {
141
+ drawing ();
142
+ haveColor = false ;
143
+ ctx .strokeStyle = canvas .backgroundColor ;
144
+ });
119
145
square .addEventListener (" click" , removeDraw);
120
146
circle .addEventListener (" click" , removeDraw);
121
147
magic .addEventListener (" click" , removeDraw);
122
148
text .addEventListener (" click" , removeDraw);
123
-
124
-
125
- })
126
-
127
- </script >
149
+ });
150
+ </script >
0 commit comments