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

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(/img/game/askway/a.jpg);

background-repeat: no-repeat;

display: none;

}

#part2 {

border: solid 1px blue;

width: 90px;

height: 68px;

background-image: url(/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(/img/game/askway/b.png);

background-repeat: no-repeat;

background-attachment: fixed;

background-position: -118px -0px;

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

}

canvas元素示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A: 对于实现图画,我们可以利用C语言的图形库来完成。这里我以Windows系统下的WinAPI为例,介绍一下具体实现方法。 首先,我们需要使用WinAPI中的CreateWindow函数创建一个窗口: ``` HWND hwnd = CreateWindow(TEXT("myWindow"), TEXT("My Dynamic Drawing"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ``` 其中,第一个参数指定窗口类名;第二个参数指定窗口标题;第三个参数指定窗口风格;第四至七个参数指定窗口位置和大小;最后四个参数可忽略。 之后,我们需要注册一个窗口类,以便系统可以识别我们创建的窗口。代码如下: ``` WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = TEXT("myWindow"); RegisterClass(&wc); ``` 其中,WndProc是我们自定义的窗口处理函数,需要在后面编写。 接下来,我们需要在窗口中绘制图形。我们可以在窗口绘制时触发WM_PAINT消息,在其中进行绘制。 ``` case WM_PAINT: { HDC hdc; PAINTSTRUCT ps; hdc = BeginPaint(hwnd, &ps); // 在hdc中进行绘制 EndPaint(hwnd, &ps); } break; ``` 在绘制图形时,我们可以使用Windows GDI(图形设备接口)库中的函数。比如,我们可以使用MoveToEx和LineTo函数绘制一个直线: ``` MoveToEx(hdc, 100, 100, NULL); LineTo(hdc, 200, 200); ``` 在态绘制中,我们可以使用SetTimer函数定时更新窗口上的图形: ``` SetTimer(hwnd, 1, 50, NULL); ``` 其中,第一个参数指定要接收定时器消息的窗口句柄;第二个参数指定定时器ID(可以用于取消定时器);第三个参数指定定时器间隔时间(单位:毫秒);最后一个参数为NULL。 在我们自定义的窗口处理函数WndProc中,需要处理WM_TIMER消息,进行图形的更新。比如,我们可以平移直线的终点位置: ``` case WM_TIMER: { xEnd += 2; yEnd += 2; InvalidateRect(hwnd, NULL, TRUE); } break; ``` 其中,xEnd和yEnd为直线终点的坐标。 最后,我们需要在窗口关闭时,释放资源并取消定时器: ``` case WM_DESTROY: KillTimer(hwnd, 1); PostQuitMessage(0); break; ``` 完整代码如下: ``` #include <Windows.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // 创建窗口 HWND hwnd = CreateWindow(TEXT("myWindow"), TEXT("My Dynamic Drawing"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); // 注册窗口类 WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = TEXT("myWindow"); RegisterClass(&wc); // 显示窗口 ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // 设置定时器 SetTimer(hwnd, 1, 50, NULL); // 消息循环 MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static int xEnd = 100, yEnd = 100; switch (msg) { case WM_PAINT: { HDC hdc; PAINTSTRUCT ps; hdc = BeginPaint(hwnd, &ps); MoveToEx(hdc, 100, 100, NULL); LineTo(hdc, xEnd, yEnd); EndPaint(hwnd, &ps); } break; case WM_TIMER: { xEnd += 2; yEnd += 2; InvalidateRect(hwnd, NULL, TRUE); } break; case WM_DESTROY: { KillTimer(hwnd, 1); PostQuitMessage(0); } break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } ``` 运行代码,我们可以看到一个画出不断移的线段的窗口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值