canvas中的drawImage

来源:https://www.cnblogs.com/hxblogs/archive/2020/07/11/13284972.html
-Advertisement-
Play Games

一、在canvas畫布中如何載入圖片 用drawImage( )方法 drawImage用法的三種情況: 1、在畫布指定位置定義圖像 ctx.drawImage(img,x,y); 註:此時畫布上顯示的圖片大小是圖片的預設大小 2、在畫布上定點陣圖像,並規定圖像的寬度和高度: ctx.drawImag ...


一、在canvas畫布中如何載入圖片

---用drawImage( )方法

drawImage用法的三種情況:

1、在畫布指定位置定義圖像

ctx.drawImage(img,x,y);

註:此時畫布上顯示的圖片大小是圖片的預設大小

2、在畫布上定點陣圖像,並規定圖像的寬度和高度:

ctx.drawImage(img,x,y,width,height);

3、剪切圖像,併在畫布上定位被剪切的部分:

ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);

參數值

 

 

4、載入圖片的兩種方法

  (1)、document.getElementById(' img ');

  (2)、var image = new Image( ) ;

 

二、在畫布中心載入部分圖片

 

如上圖、載入第一行的第一個小怪獸

 <script>
        var Person = function(canvas){
          this.ctx = document.querySelector("canvas").getContext("2d");
          this.canvasWidth = this.ctx.canvas.width;
          this.canvasHeight = this.ctx.canvas.height;
          this.src = "image/04.png";
          this.init();
        }
        Person.prototype.init = function(){
          var that = this;
          this.loadImage(function(image){
            // 獲取圖片的寬高
            that.imageWidth = image.width;
            that.imageHeight = image.height;
            // 獲取單個小怪獸區域的寬高
            that.positionWidth = that.imageWidth / 4;
            that.positionHeight = that.imageHeight / 4;

            // 預設是從左上角顯示的 
            that.x0 = that.canvasWidth / 2 - that.positionWidth / 2;
            that.y0 = that.canvasHeight / 2 - that.positionHeight / 2;

            // 繪製圖片
            that.ctx.drawImage(image, 0, 0, that.positionWidth, that.positionHeight, that.x0,
              that.y0, that.positionWidth, that.positionHeight);

          })
        }
        Person.prototype.loadImage = function(callback){
          var image = new Image();
          // 圖片載入完成後執行
          image.onload = function(){
            callback && callback(image);
          }
          image.src = this.src;
        }

        var person = new Person();
      </script>

  

 

三、繪製小人行走的幀動畫

<s<script>
        var Person = function(canvas){
          this.ctx = document.querySelector("canvas").getContext("2d");
          this.canvasWidth = this.ctx.canvas.width;
          this.canvasHeight = this.ctx.canvas.height;
          this.src = "image/04.png";
          this.init();
        }
        Person.prototype.init = function(){
          var that = this;
          this.loadImage(function(image){
            // 獲取圖片的寬高
            that.imageWidth = image.width;
            that.imageHeight = image.height;
            // 獲取單個小怪獸區域的寬高
            that.positionWidth = that.imageWidth / 4;
            that.positionHeight = that.imageHeight / 4;

            // 預設是從左上角顯示的 
            that.x0 = that.canvasWidth / 2 - that.positionWidth / 2;
            that.y0 = that.canvasHeight / 2 - that.positionHeight / 2;

            // 繪製圖片
            that.ctx.drawImage(image, 0, 0, that.positionWidth, that.positionHeight, that.x0,
              that.y0, that.positionWidth, that.positionHeight);
            var index = 0;
            setInterval(function(){
              that.ctx.clearRect(0, 0, that.canvasWidth, that.canvasHeight);
              index++;
              that.ctx.drawImage(image, index * that.positionWidth, 0, that.positionWidth, that.positionHeight, that.x0, that.y0,
         that.positionWidth, that.positionHeight); if(index >= 3){ index = 0; } }, 100); }) } Person.prototype.loadImage = function(callback){ var image = new Image(); // 圖片載入完成後執行 image.onload = function(){ callback && callback(image); } image.src = this.src; } var person = new Person(); </script>

  

四、繪製疾走的小怪獸

  可以通過鍵盤上下左右鍵控制小人在畫布中任意行走

   

 <<script>
        var Person = function(canvas){
          this.ctx = document.querySelector("canvas").getContext("2d");
          this.canvasWidth = this.ctx.canvas.width;
          this.canvasHeight = this.ctx.canvas.height;
          this.stepX = 0;
          this.stepY = 0;
          this.stepSize = 10;
          this.index = 0;
          this.direction = 0;
          this.src = "image/04.png";
          this.init();
        }
        Person.prototype.init = function(){
          var that = this;
          this.loadImage(function(image){
            // 獲取圖片的寬高
            that.imageWidth = image.width;
            that.imageHeight = image.height;
            // 獲取單個小怪獸區域的寬高
            that.positionWidth = that.imageWidth / 4;
            that.positionHeight = that.imageHeight / 4;

            // 預設是從左上角顯示的 
            that.x0 = that.canvasWidth / 2 - that.positionWidth / 2;
            that.y0 = that.canvasHeight / 2 - that.positionHeight / 2;

            // 繪製圖片
            that.ctx.drawImage(image, 0, 0, that.positionWidth, that.positionHeight, that.x0,
              that.y0, that.positionWidth, that.positionHeight);
            var index = 0;
            document.onkeydown = function(e){
              that.ctx.clearRect(0, 0, that.canvasWidth, that.canvasHeight);
              switch(e.keyCode){
                case 37 : 
                  console.log('左');
                  that.direction = 1;
                  that.stepX--;
                  that.showImage(image);
                  break;
                case 38 : 
                  console.log('上');
                  that.direction = 3;
                  that.stepY--;
                  that.showImage(image);
                  break;
                case 39 : 
                  console.log('右');
                  that.direction = 2;
                  that.stepX++;
                  that.showImage(image);
                  break;
                case 40 : 
                  console.log('下');
                  that.direction = 0;
                  that.stepY++;
                  that.showImage(image);
                  break;

              }
            }
           
          })
        }
        Person.prototype.loadImage = function(callback){
          var image = new Image();
          // 圖片載入完成後執行
          image.onload = function(){
            callback && callback(image);
          }
          image.src = this.src;
        }
        Person.prototype.showImage = function(image){
          this.index++;
          console.log(this.index);
           this.ctx.drawImage(image, this.index * this.positionWidth, this.direction * this.positionHeight, this.positionWidth, this.positionHeight, this.x0 + this.stepX * this.stepSize, this.y0 + this.stepY * this.stepSize, this.positionWidth, this.positionHeight);
           if(this.index >= 3){
             this.index = 0;
           }
        }
        var person = new Person();
      </script>

  

   

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 容器是一種清理級、可移植、自包含的軟體打包技術,使應用程式可以在幾乎任何地方以相同的方式運行。 ...
  • 容器是一種清理級、可移植、自包含的軟體打包技術,使應用程式可以在幾乎任何地方以相同的方式運行。 ...
  • Modbus的CRC校驗實驗 表述 Name : "CRC-16/MODBUS" Width : 16 Poly : 8005 Init : FFFF RefIn : True RefOut : True XorOut : 0000 Check : ? 先參考一些廠家給的MODBUS校驗程式 con ...
  • 本文更新於2020-06-14,使用MySQL 5.7,操作系統為Deepin 15.4。 在指定參數選項時有兩種方式: “-”+單個縮寫字元+值,值前可加上空白符(“-p”選項例外,若加上則認為稍後輸入密碼,空白後面的值並不被認作密碼)。 “--”+完整單詞+“=”+值 所有工具會使用一些通用選項 ...
  • SQL自學筆記 事務 1.0事務的基本介紹 2.0 事務的自動提交&手動提交 3.0 事務的四大特征 4.0 事務的隔離級別 DCL ...
  • 一、使用本地廣播 1.本地廣播一個舉例 package com.example.broadcasttest2; import android.app.Activity; import android.content.BroadcastReceiver; import android.content. ...
  • 運行環境:Xcode 11.1 Swift5.0 最近參與的一個項目需要從Objective-C(以下簡稱OC)轉到Swift,期間遇到了一些坑,於是有了這篇總結性的文檔。 如果你也有將OC項目Swift化的需求,可以作為參考。 OC轉Swift有一個大前提就是你要對Swift有一定的瞭解,熟悉Sw ...
  • 最近一直被win10彈出的“修複你的工作或學校賬戶”煩擾, 點進去發現原來是之前公司的賬號還在win10下,和我的微軟賬戶綁定在一起, 百度了一堆都沒有可用的刪除方法。 最後發現在 <設置> > <連接工作或學校賬戶> 中有此公司賬號,並且下麵有一個按鈕<斷開連接>: 這裡斷開連接就行,我這裡已經刪 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...