11<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 >
312 <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 >
1615 </div >
17- </body >
16+ </div >
17+ <ToolButtons />
18+ </body >
1819</template >
1920
2021<style >
21-
22- canvas {
23- background-color : white ;
22+ canvas {
23+ background-color : #ffffff ;
2424 border : 2px solid gray ;
2525 border-radius : 2px ;
2626 margin-bottom : 10px ;
27- }
28-
27+ }
2928
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+ }
3033 </style >
3134
3235<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" ;
3538
36- export default {
37- name: ' NewBadge' ,
39+ export default {
40+ name: " NewBadge" ,
3841 components: {
39- DrawButtons,
40- ToolButtons,
41- }
42+ DrawButtons,
43+ ToolButtons
4244 }
45+ };
4346
44-
45-
46- window .addEventListener (" load" , () => {
47+ window .addEventListener (" load" , () => {
4748 const canvas = document .getElementById (" canvas" );
4849
50+ canvas .backgroundColor = " #ffffff" ;
51+ let haveColor = true ;
52+ let pencilColor;
53+
4954 const ctx = canvas .getContext (" 2d" );
5055
5156 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+ };
5763 }
5864
5965 ctx .strokeStyle = " #000000" ;
6268
6369 let shouldPaint = false ;
6470
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+
6579 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 );
7185 }
7286
7387 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+ }
7993 }
8094
8195 function endDrawing () {
82- shouldPaint = false ;
96+ shouldPaint = false ;
8397 }
8498
8599 // Allows user to draw on canvas
86100 drawing ();
87101
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);
92113 }
93114
94115 // Changing colors
95116 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+ });
100124
101125 // Changing brush size
102126 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+ });
109131
110132 // 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);
114135 }
115136
116137 selector .addEventListener (" click" , removeDraw);
117138 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+ });
119145 square .addEventListener (" click" , removeDraw);
120146 circle .addEventListener (" click" , removeDraw);
121147 magic .addEventListener (" click" , removeDraw);
122148 text .addEventListener (" click" , removeDraw);
123-
124-
125- })
126-
127- </script >
149+ });
150+ </script >
0 commit comments