-
Notifications
You must be signed in to change notification settings - Fork 0
/
try.html
78 lines (68 loc) · 1.75 KB
/
try.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
<!DOCTYPE HTML>
<html>
<head>
<style>
*{margin:0;padding:0;font-family:sans-serif;background: #c0ffee}
canvas{background:#fff;}
a {
background: #69c;
color: #fff;
padding: 5px 10px;
}
</style>
</head>
<body>
Paint by pressing your mouse and moving it. Download with the button below.
<canvas></canvas>
</body>
</html>
<script>// grab the canvas element, get the context for API access and
// preset some variables
var canvas = document.querySelector( 'canvas' ),
c = canvas.getContext( '2d' ),
mouseX = 0,
mouseY = 0,
width = 300,
height = 300,
colour = 'hotpink',
mousedown = false;
// resize the canvas
canvas.width = width;
canvas.height = height;
function draw() {
if (mousedown) {
// set the colour
c.fillStyle = colour;
// start a path and paint a circle of 20 pixels at the mouse position
c.beginPath();
c.arc( mouseX, mouseY, 10 , 0, Math.PI*2, true );
c.closePath();
c.fill();
}
}
// get the mouse position on the canvas (some browser trickery involved)
canvas.addEventListener( 'mousemove', function( event ) {
if( event.offsetX ){
mouseX = event.offsetX;
mouseY = event.offsetY;
} else {
mouseX = event.pageX - event.target.offsetLeft;
mouseY = event.pageY - event.target.offsetTop;
}
// call the draw function
draw();
}, false );
canvas.addEventListener( 'mousedown', function( event ) {
mousedown = true;
}, false );
canvas.addEventListener( 'mouseup', function( event ) {
mousedown = false;
}, false );
var link = document.createElement('a');
link.innerHTML = 'download image';
link.addEventListener('click', function(ev) {
link.href = canvas.toDataURL();
link.download = "mypainting.png";
}, false);
document.body.appendChild(link);
</script>