例子3:绘制直线,并且用直线组合复杂图形
1. /**
2. * This file is confidential by Charles.Wang
3. * Copyright belongs to Charles.wang
4. * You can make contact with Charles.Wang (charles_wang888@126.com)
5. */
6.
7. function drawLine(id){
8.
9. var canvas=document.getElementById(id);
10. if(canvas==null)
11. return false;
12. var context = canvas.getContext('2d');
13. context.fillStyle="#FF00FF";
14. context.fillRect(0,0,400,300);
15.
16. var n=0;
17. var dx=150;
18. var dy=150;
19. var s = 100;
20. context.beginPath();
21. context.fillStyle='rgb(100,255,100)';
22. context.strokeStyle='rgb(0,0,100)';
23. var x = Math.sin(0);
24. var y = Math.cos(0);
25. var dig=Math.PI/15*11;
26. for(var i = 0;i<30;i++){
27. var x = Math.sin(i*dig);
28. var y = Math.cos(i*dig);
29. //用三角函数计算顶点
30. context.lineTo(dx+x*s,dy+y*s);
31. }
32. context.closePath();
33. context.fill();
34. context.stroke();
35. }