-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvansTest2
67 lines (62 loc) · 1.71 KB
/
canvansTest2
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
<!doctype html>
<html>
<head>
<title>canvasTest1</title>
<style rel="">
#test2 {
margin: 30px auto;
w idth: 400px;
he ight: 400px;
border: 5px solid #000;
}
</style>
<script>
window.onload=function() {
var mycanvas = document.getElementById("test2")
var mycontext = mycanvas.getContext('2d');
var x;
var y;
var x2;
var y2;
var r;
var g;
var b,cw,ch;
var oImg = new Image();
oImg.src = './images/1.jpg';
oImg.onload =function(){
var w = oImg.width,
h = oImg.height;
cw = mycanvas.offsetWidth,
ch = mycanvas.offsetHeight;
console.log(cw + '::' + h)
mycontext.drawImage(oImg, 0,0,w,h, Math.round(cw/2-w/2), Math.round(ch/2-h/2), w,h)
}
function line() {
x=Math.floor(Math.random()*190) + Math.floor(Math.random()*190);
y=Math.floor(Math.random()*190) + Math.floor(Math.random()*190);
x2=Math.floor(Math.random()*190) + Math.floor(Math.random()*190);
y2=Math.floor(Math.random()*190) + Math.floor(Math.random()*190);
r=Math.floor(Math.random()*255);
g=Math.floor(Math.random()*255);
b=Math.floor(Math.random()*255);
mycontext.clearRect(0, 0, cw, ch);
mycontext.beginPath();
mycontext.moveTo(x, y);
mycontext.lineTo(x2, y2);
mycontext.strokeStyle='rgb(' + r + ',' + g + ',' + b + ')';
mycontext.lineWidth=Math.floor(Math.random()*6);
mycontext.stroke();
mycontext.closePath();
//mycontext.restore();
}
setInterval(line, 100);
}
</script>
</head>
<body>
<canvas id="test2" width="800" height="600" style="border: 5px blue solid"
>
你的浏览器比支持canvas!
</canvas>
</body>
</html>