html js how to draw on screen

Solutions on MaxInterview for html js how to draw on screen by the best coders in the world

showing results for - "html js how to draw on screen"
Emie
19 Mar 2017
1 <html>
2    <script type="text/javascript">
3    var canvas, ctx, flag = false,
4        prevX = 0,
5        currX = 0,
6        prevY = 0,
7        currY = 0,
8        dot_flag = false;
9
10    var x = "black",
11        y = 2;
12    
13    function init() {
14        canvas = document.getElementById('can');
15        ctx = canvas.getContext("2d");
16        w = canvas.width;
17        h = canvas.height;
18    
19        canvas.addEventListener("mousemove", function (e) {
20            findxy('move', e)
21        }, false);
22        canvas.addEventListener("mousedown", function (e) {
23            findxy('down', e)
24        }, false);
25        canvas.addEventListener("mouseup", function (e) {
26            findxy('up', e)
27        }, false);
28        canvas.addEventListener("mouseout", function (e) {
29            findxy('out', e)
30        }, false);
31    }
32    
33    function color(obj) {
34        switch (obj.id) {
35            case "green":
36                x = "green";
37                break;
38            case "blue":
39                x = "blue";
40                break;
41            case "red":
42                x = "red";
43                break;
44            case "yellow":
45                x = "yellow";
46                break;
47            case "orange":
48                x = "orange";
49                break;
50            case "black":
51                x = "black";
52                break;
53            case "white":
54                x = "white";
55                break;
56        }
57        if (x == "white") y = 14;
58        else y = 2;
59    
60    }
61    
62    function draw() {
63        ctx.beginPath();
64        ctx.moveTo(prevX, prevY);
65        ctx.lineTo(currX, currY);
66        ctx.strokeStyle = x;
67        ctx.lineWidth = y;
68        ctx.stroke();
69        ctx.closePath();
70    }
71    
72    function erase() {
73        var m = confirm("Want to clear");
74        if (m) {
75            ctx.clearRect(0, 0, w, h);
76            document.getElementById("canvasimg").style.display = "none";
77        }
78    }
79    
80    function save() {
81        document.getElementById("canvasimg").style.border = "2px solid";
82        var dataURL = canvas.toDataURL();
83        document.getElementById("canvasimg").src = dataURL;
84        document.getElementById("canvasimg").style.display = "inline";
85    }
86    
87    function findxy(res, e) {
88        if (res == 'down') {
89            prevX = currX;
90            prevY = currY;
91            currX = e.clientX - canvas.offsetLeft;
92            currY = e.clientY - canvas.offsetTop;
93    
94            flag = true;
95            dot_flag = true;
96            if (dot_flag) {
97                ctx.beginPath();
98                ctx.fillStyle = x;
99                ctx.fillRect(currX, currY, 2, 2);
100                ctx.closePath();
101                dot_flag = false;
102            }
103        }
104        if (res == 'up' || res == "out") {
105            flag = false;
106        }
107        if (res == 'move') {
108            if (flag) {
109                prevX = currX;
110                prevY = currY;
111                currX = e.clientX - canvas.offsetLeft;
112                currY = e.clientY - canvas.offsetTop;
113                draw();
114            }
115        }
116    }
117    </script>
118    <body onload="init()">
119        <canvas id="can" width="400" height="400" style="position:absolute;top:10%;left:10%;border:2px solid;"></canvas>
120        <div style="position:absolute;top:12%;left:43%;">Choose Color</div>
121        <div style="position:absolute;top:15%;left:45%;width:10px;height:10px;background:green;" id="green" onclick="color(this)"></div>
122        <div style="position:absolute;top:15%;left:46%;width:10px;height:10px;background:blue;" id="blue" onclick="color(this)"></div>
123        <div style="position:absolute;top:15%;left:47%;width:10px;height:10px;background:red;" id="red" onclick="color(this)"></div>
124        <div style="position:absolute;top:17%;left:45%;width:10px;height:10px;background:yellow;" id="yellow" onclick="color(this)"></div>
125        <div style="position:absolute;top:17%;left:46%;width:10px;height:10px;background:orange;" id="orange" onclick="color(this)"></div>
126        <div style="position:absolute;top:17%;left:47%;width:10px;height:10px;background:black;" id="black" onclick="color(this)"></div>
127        <div style="position:absolute;top:20%;left:43%;">Eraser</div>
128        <div style="position:absolute;top:22%;left:45%;width:15px;height:15px;background:white;border:2px solid;" id="white" onclick="color(this)"></div>
129        <img id="canvasimg" style="position:absolute;top:10%;left:52%;" style="display:none;">
130        <input type="button" value="save" id="btn" size="30" onclick="save()" style="position:absolute;top:55%;left:10%;">
131        <input type="button" value="clear" id="clr" size="23" onclick="erase()" style="position:absolute;top:55%;left:15%;">
132    </body>
133    </html>