怎么用HTML实现人物吃饭,使用js实现图画人物动起来的方法

本文介绍了一个使用HTML5 Canvas元素进行绘图的示例,包括绘制填充的矩形、边框及图片显示,并通过JavaScript实现动态更新背景位置和重画图片的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pageEncoding="UTF-8"%>

canvas元素示例

function draw(id) {

var canvas = document.getElementById(id);

if (canvas == null)

return false;

var context = canvas.getContext('2d');

context.fillStyle = "#EEEEFF"; //設置顏色

context.fillRect(0, 0, 400, 300); //畫正方形

context.fillStyle = "red"; //顏色

context.strokeStyle = "blue"; //邊框顏色

context.lineWidth = 1;//邊框線寬度

context.fillRect(50, 50, 100, 100);

context.strokeRect(50, 50, 100, 100);

var img = new Image();

img.src="/img/game/askway/a.jpg";

context.drawImage(img,0,0);

setInterval(move,100);

}

var j=1;

function move()

{

$("#part3").css("background-position",-(118 * (j+1)+70*j)+"px -0px");

j++;

if(j==6){

j=0;

}

}

var i=50;

function rehua(){

setInterval(redraw,30);

}

function redraw(){

//var context = $("#canvas").getContext("2d");

var context = document.getElementById("canvas").getContext("2d");

context.clearRect(0, 0, 400, 300);

var img = new Image();

img.src="/img/game/askway/a.jpg";

context.drawImage(img, i, i);

i++;

}

#part1 {

border: solid 1px blue;

width: 90px;

height: 68px;

background-image: url(/https/blog.csdn.net/img/game/askway/a.jpg);

background-repeat: no-repeat;

display: none;

}

#part2 {

border: solid 1px blue;

width: 90px;

height: 68px;

background-image: url(/https/blog.csdn.net/img/game/askway/a.jpg);

background-repeat: no-repeat;

background-position: 50% 50%;

display: none;

}

#part3 {

border: solid 1px blue;

width: 70px; /* 960 576 192 192*/

height: 100px;

background-image: url(/https/blog.csdn.net/img/game/askway/b.png);

background-repeat: no-repeat;

background-attachment: fixed;

background-position: -118px -0px;

/* background-position:10% 5%; */

}

canvas元素示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值