首先是html代码(其实就只有一个画布,记得要把外部js引入写在body底部
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text" charset="utf-8" />
</head>
<body>
<canvas id="one" height="1000" width="1000"></canvas>
<script src="./weixin.js"></script>
</body>
</html>
圆中写文字用到 文字的定位

用textBaseline定位高度在中间,

用textAlign定位宽度在中间

以下代码画了有边缘的圆及文字

var c=document.getElementById("one");
var ctx=c.getContext("2d");
ctx.strokeStyle="black";
ctx.beginPath();
ctx.fillStyle="lightgreen";
ctx.arc(100,100,50,0,Math.PI*2,false);
ctx.stroke();
ctx.fill(); ctx.beginPath();
ctx.fillStyle="white";
ctx.font="40px Arial";
ctx.textBaseline="middle";
ctx.textAlign="center";
ctx.fillText("He",100,100);
ctx.strokeText("He",100,100);

circle

以下是随机运动的圆(不过这个圆和文字是没有边缘线的,因为我觉得加边缘线太丑了),令人苦笑不得的是最后它们都停留在了画布边缘..

var c = document.getElementById("one");
var ctx = c.getContext("2d");
var arrHe = [];
var arrNe = [];
var numHe = 100;
var numNe = 5; //随机点
for (var i = 0; i < numHe; i++) {
arrHe.push({
x: rnd(c.width,50),
y: rnd(c.height,50),
speedX: rndSign() * rnd(1,0),
speedY: rndSign() * rnd(1,0)
});
} setInterval(
function (){
ctx.clearRect(0, 0, c.width, c.height);
//He绘画
arrHe.forEach(
function(dot) {
var {
x,
y,
speedX,
speedY
} = dot;
ctx.beginPath();
ctx.fillStyle = "lightgreen";
ctx.arc(dot.x, dot.y, 50, 0, Math.PI * 2, false);
ctx.fill(); ctx.beginPath();
ctx.fillStyle = "white";
ctx.font = "40px Arial";
ctx.textBaseline = "middle";
ctx.textAlign = "center";
ctx.fillText("He", dot.x, dot.y); if (dot.x < 0 || dot.x > c.width) {
speedX *= -1;
}
if (dot.y < 0 || dot.y > c.height) {
speedY *= -1;
}
dot.x += speedX;
dot.y += speedY; });
}
,16); //生成随机点位置和随机方向
function rnd(m,n) {
return Math.random() * (m-n);
} function rndSign() {
return Math.random() >0.5 ? 1:-1;
}

move

array.forEach(function(currentValue, index, arr), thisValue)

currentValue 必须。当前元素。

index 可选。当前元素的索引值。

arr 可选。当前元素所属的数组对象。

thisValue 可选。不填时,默认为this

最新文章

  1. 【原创】Chrome最新版(53-55)再次爆出BUG!
  2. Mysql5.0没有nvarchar,national
  3. php 错误处理函数
  4. web系统架构设计中需要知道的点(前端篇)
  5. zk FileUpload(文件上传)
  6. Java的自动装箱和拆箱的简单讲解
  7. awk命令
  8. mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干
  9. Ubuntu kylin系统改中文系统文件名为英文
  10. AppWidget框架
  11. ASP.NET中的母版页机制
  12. 使用Lucene.net提升网站搜索速度整合记录
  13. SaaS系列介绍之十五: SaaS知识重用
  14. JavaScript学习笔记(2)——JavaScript和DOM的关系
  15. 403. Frog Jump
  16. struct和typedef struct的用法
  17. UVa12304
  18. php 函数strtr 替换函数实例解析 strtr 速度比较快
  19. QtQuick桌面应用程序开发指导 3)达到UI而功能_B 4)动态管理Note物_A
  20. SuperWebClient -一个基于CURL的.NET HTTP/HTTPS模拟神组件(1)

热门文章

  1. FPGA基础学习(4) -- 时序约束(理论篇)
  2. EasyUI学习笔记(三)—— message和menubutton的使用
  3. python学习之路---day06
  4. Jenkins 更换国内源
  5. windows 下 redis 安装
  6. HDU - 4825 01字典树套路题
  7. sed 练习
  8. MongoDB 配置服务
  9. IOS下去掉input submit圆角和
  10. PHP foreach ($arr as &amp;amp;$value)