html5之Canvas路径绘图、坐标变换应用实例
来源:易贤网 阅读:922 次 日期:2016-08-01 15:46:53
温馨提示:易贤网小编为您整理了“html5之Canvas路径绘图、坐标变换应用实例”,方便广大网友查阅!

本文写一个html5的时钟应用程序试手主要设置了坐标变换的平移(translate)和旋转变换(ratate),以及html5 Canvas的路径绘图,beginPath,closePath,rect,arc等多的也不说了,直接上代码有需要的朋友可以参考下

代码如下:

<html>

<head>

</head>

<body>

<canvas id="myCanvas" width="600" height="300">你的浏览器还不支持哦</canvas>

<script type="text/javascript">

var c = document.getElementById("myCanvas");

var cxt = c.getContext("2d");

var slen = 60;

var mlen = 50;

var hlen = 40;

cxt.strokeRect(0, 0, c.width, c.height);

cxt.beginPath();

cxt.strokeStyle = "#00f";

cxt.fillStyle = "#00f";

cxt.arc(200, 150, 5, 0, 2 * Math.PI, true);

cxt.fill();

cxt.closePath();

cxt.beginPath();

cxt.strokeStyle = "#00f";

cxt.arc(200, 150, 100, 0, 2 * Math.PI, true);

cxt.stroke();

cxt.closePath();

cxt.beginPath();

cxt.translate(200, 150); //平移原点;

cxt.rotate(-Math.PI / 2);

cxt.save();

for (var i = 0; i < 60; i++) {

if (i % 5 == 0) {

// cxt.fillStyle = "#ff0000";

cxt.fillRect(80, 0, 20, 5);

cxt.fillText("" + (i / 5 == 0 ? 12 : i / 5), 70, 0);

} else {

// cxt.strokeStyle = "#00f";

cxt.fillRect(90, 0, 10, 2);

}

//document.getElementById("div1").innerText += " " + i;

cxt.rotate(Math.PI / 30);

}

cxt.closePath();

var ls = 0, lm = 0, lh = 0;

function Refresh() {

cxt.restore();

cxt.save();

cxt.rotate(ls * Math.PI / 30);

cxt.clearRect(5, -1, slen+1, 2+2);

cxt.restore(); cxt.save();

cxt.rotate(lm * Math.PI / 30);

cxt.clearRect(5, -1, mlen+1, 3+2);

cxt.restore(); cxt.save();

cxt.rotate(lh * Math.PI / 6);

cxt.clearRect(5, -3, hlen+1, 4+2);

var time = new Date();

var s = ls=time.getSeconds();

var m = lm=time.getMinutes();

var h = lh=time.getHours();

cxt.restore();

cxt.save();

cxt.rotate(s * Math.PI / 30);

cxt.fillRect(5, 0, slen, 2);

cxt.restore(); cxt.save();

cxt.rotate(m * Math.PI / 30);

cxt.fillRect(5, 0, mlen, 3);

cxt.restore(); cxt.save();

cxt.rotate(h * Math.PI / 6);

cxt.fillRect(5, -2, hlen, 4);

}

var MyInterval = setInterval("Refresh();", 1000);

</script>

<div id="div1" style=" background:#00f;"></div>

</body>

</html>

更多信息请查看网页制作
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标