HMTL5的学习断断续续,方法不用又生疏了,昨天做的一个雨伞的Demo,先看看效果

主要是运用了中心点变换和旋转两个方法。不同的动画用定时器控制,

下面是全部代码:

  <canvas id="myCanve" width="1200" height="600"></canvas>
<div id="ta"></div>
<script>
var ctx;
var everything = [];
var cwidth = 1200;
var cheight = 600;
var updowntime = 0;
var rotatetime = 0;
var my = 2;
var updown;
var rotateangle = 0;

function Umbrellas(sx, sy, fillStyle) {
this.x = sx;
this.y = sy;
this.fillStyle = fillStyle;
this.draw = drawUmbrella;
this.moveit = moveumbrella;
}

function drawUmbrella() {
//先画半圆
ctx.fillStyle = this.fillStyle;
ctx.beginPath();
ctx.arc(
this.x, this.y, 30, 0, Math.PI, true);
ctx.closePath();
ctx.fill();

// ctx.save();
ctx.fillStyle = "blue";
ctx.fillRect(
this.x - 1.5, this.y, 1.5, 40);//画伞柄 是一个细长的矩形
ctx.beginPath();
ctx.strokeStyle
= "blue";
ctx.arc(
this.x - 5, this.y + 40, 4, Math.PI, Math.PI * 2, true);//伞的钩子 是一个半圆
ctx.stroke();
ctx.closePath();
}

function moveumbrella(dx, dy) {
this.x += dx;
this.y += dy;
}

function init() {
ctx
= document.getElementById("myCanve").getContext('2d');
ctx.translate(
200, 300);
for (var i = 0; i < 10; i++) {
var um = new Umbrellas(i * 70, 0, "rgb(" + (30 * i) + "," + (255 - 30 * i) + ",255)");
everything.push(um);
}

updown
= setInterval(change, 100);
setTimeout(
function () {
clearInterval(updown);
ctx.translate(
300,0);//改变中心点
setInterval(rotat,
100);
},
10000);
}

//updown
function change() {
if (updowntime % 50 == 0) {
my
= -my;
}
// 清屏要关注到原中心点
clear(-200,-300);
updowntime
++;

for (var i = 0; i < everything.length; i++) {
if (i % 2 == 0) {
everything[i].moveit(
0, my);
}
else {
everything[i].moveit(
0, -my);
}
everything[i].draw();
}

$(
"#ta").html("坐标y:" + everything[0].y + "______updowntime:" + updowntime);

//放在这才有效 在执行方法里面停止
if (updowntime == 100) {
clearInterval(updown);
//过渡方法
}
}

//选择
function rotat() {
clear(
-400, -300);
rotatetime
++;


for (var i = 0; i < everything.length; i++) {
ctx.save();
ctx.rotate(Math.PI
* (2 / 4 + i / 4));
ctx.translate(
0, rotateangle);
// 统一坐标
everything[i].x = 0;
everything[i].y
= 0;
everything[i].draw();
ctx.restore();
}
rotateangle
++;
$(
"#ta").html("角度:" + rotateangle + " rotatetime:" + rotatetime);

if (rotateangle == 70) {
rotateangle
= -rotateangle;
}

}

function clear(x,y) {
ctx.clearRect(x, y,
1200, 600);
}


window.onload
= function () {
init();
}
</script>

更多相关文章

  1. html中插入activex控件之后 需要在 网页中实现控件的属性及方法,
  2. js报错:****is not a function 的解决方法
  3. ExtJS的使用方法汇总(4)——拖放以及弹出窗口
  4. ajax提交含有html数据时的处理方法
  5. Android里string.xml使用html标签的方法
  6. 在AppCode中的razor调用HtmlHelper方法和UrlHelper方法
  7. 前端html数组去重的方法
  8. html解析类库htmlparser.net使用方法
  9. Ubuntu的FireFox无法使用HTML5播放器的解决方法

随机推荐

  1. android EditText中的inputType .
  2. android 环境搭建helloworld
  3. android移动数据上网的开关的实现
  4. 腾讯微博客户端开发视频--若水(第三集、第
  5. Android SDK 快速安装方法
  6. android获取APK签名信息及MD5指纹
  7. 【面包屑】快速使用RecyclerView搭建列表
  8. 修改ListView的分割线
  9. Android之Handler详解(四)
  10. Android LayoutInflater