九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
使用canvas完成幀動畫(方向鍵控制行走的小人)

根據(jù)此精靈圖設(shè)置動圖

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <meta name="viewport" content="width=device-width, initial-scale=1.0">  <title>Document</title>  <style>    body {      margin: 0;      padding: 0;    }    canvas {      display: block;      margin: 100px auto;      position: relative;    }  </style></head><body> <canvas width="600" height="400" style="border: 1px solid gray;"></canvas>  <script>    var Persion = function (ctx) {      this.ctx = ctx || document.querySelector('canvas').getContext('2d');      this.src = "../img/03.png";      //獲取畫布大小      this.canvasWidth = this.ctx.canvas.width;      this.canvasHeight = this.ctx.canvas.height;      // 行走參數(shù)      this.stepSzie = 10;      // 0 前 1 左 2 右 3 后      this.direction = 0;      this.stepX = 0;      this.stepY = 0;      this.init();    }    Persion.prototype.init = function () {      var that = this;        /*1.加載圖片*/        this.loadImage(function (image) {            /*圖片的大小*/            that.imageWidth = image.width;            that.imageHeight = image.height;            /*人物的大小*/            that.personWidth = that.imageWidth / 4;            that.personHeight = that.imageHeight / 4;            /*繪制圖片的起點*/            that.x0 = that.canvasWidth / 2 - that.personWidth / 2;            that.y0 = that.canvasHeight / 2 - that.personHeight / 2;            /*2.默認繪制在中心位置正面朝外*/            that.ctx.drawImage(image,                0,0,                that.personWidth,that.personHeight,                that.x0,that.y0,                that.personWidth,that.personHeight);        // 通過方向鍵控制人物行走        that.index = 0;        document.onkeydown = function (e) {          if (e.keyCode == 40) {            that.direction = 0;            that.stepY  ;            that.drawImage(image);            /*前*/          } else if (e.keyCode == 37) {            that.direction = 1;            that.stepX--;            that.drawImage(image);            /*左*/          } else if (e.keyCode == 39) {            that.direction = 2;            that.stepX  ;            that.drawImage(image);            /*右*/          } else if (e.keyCode == 38) {            that.direction = 3;            that.stepY--;            that.drawImage(image);            /*后*/          }        }      })    }    Persion.prototype.loadImage = function (callback) {      var image = new Image();      image.onload = function () {        callback && callback(image);      }      image.src = this.src;    }    Persion.prototype.drawImage = function (image) {      this.index  ;      // 清除畫布      this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight);      // 定位到x 索引        // y方向      this.ctx.drawImage(image,        this.index * this.personWidth, this.direction * this.personHeight,        this.personWidth, this.personHeight,        this.x0   this.stepX * this.stepSzie, this.y0   this.stepY * this.stepSzie,        this.personWidth, this.personHeight);      /*如果索引超出了 變成0*/      if (this.index >= 3) {        this.index = 0;      }    }    new Persion()  </script></body></html>
來源:https://www.icode9.com/content-4-793451.html
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Canvas學習參考文檔
HTML5-右腦開發(fā)項目-基礎(chǔ)訓練-整體感知訓練holisticPerception.html
html5 圖片旋轉(zhuǎn)
學習canvas繪圖
怎樣可以用HTML5 canvas旋轉(zhuǎn)圖片
使用Chartjs
更多類似文章 >>
生活服務(wù)
熱點新聞
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服