Canvas學(xué)習(xí)筆記之變形

Canvas 學(xué)習(xí)筆記之變形 -- by Damon

狀態(tài)保存和恢復(fù)

ctx.save()
ctx.restore()

狀態(tài)包括:

  • 當(dāng)前的移動(dòng)、旋轉(zhuǎn)、縮放
  • strokeStyle, fillStyle, globalAlpha, lineWidth, lineCap, lineJoin, miterLimit, shadowOffsetX, shadowOffsetY, shadowBlur, shadowColor, globalCompositeOperation 的值
  • 當(dāng)前的裁切路徑(cliping path)

狀態(tài)棧:
每次調(diào)用save就是入棧操作,restore就是出棧操作

ctx.translate(x, y)
ctx.rotate(ratio)
ctx.scale(x, y)
ctx.setTransform(m11, m12, m21, m22, dx, dy)
ctx.resetTransform() // == ctx.setTransform(1, 0, 0, 1, 0, 0)

m11: 水平方向的縮放
m12: 水平方向的偏移
m21: 豎直方向的偏移
m22: 豎直方向的縮放
dx: 水平方向的移動(dòng)
dy: 豎直方向的移動(dòng)

常用方法
變換前先save一下?tīng)顟B(tài),然后變形,畫(huà)完后重置

Paste_Image.png
window.onload = function() {

  translate();
  rotating();
  scale();
};

// 1
function translate() {

  var cvs = document.getElementById('my-canvas-1');
  var ctx = cvs.getContext('2d');
  cvs.height = 300;
  cvs.width = 300;

  ctx.fillRect(0, 0, 300, 300);
  for (var i = 0; i < 3; i++) {
    for (var j = 0; j < 3; j++) {
      ctx.save();
      ctx.strokeStyle = '#9CFF00';
      ctx.translate(50 + j * 100, 50 + i * 100);
      ctx.beginPath();
      ctx.arc(0, 0, 20, 0, Math.PI * 2);
      ctx.closePath();
      ctx.stroke();
      ctx.restore();
    }
  }
}
// 2
function rotating() {

  var cvs = document.getElementById('my-canvas-2');
  var ctx = cvs.getContext('2d');
  cvs.height = 300;
  cvs.width = 300;

  ctx.translate(150, 150);
  for (var i = 1; i < 7; i++) {
    for (var j = 0; j < i * 6; j++) {
      ctx.save();
      ctx.fillStyle = '#F0F';
      ctx.rotate(2 * Math.PI / 6 / i * j);
      ctx.beginPath();
      ctx.arc(20 * i, 0, 4, 0, Math.PI * 2);
      ctx.closePath();
      ctx.fill();
      ctx.restore();
    }
  }
}
// 3
function scale() {

  var cvs = document.getElementById('my-canvas-3');
  var ctx = cvs.getContext('2d');
  cvs.height = 300;
  cvs.width = 300;

  ctx.translate(150, 150);
  for (var i = 1; i < 7; i++) {
    for (var j = 0; j < i * 6; j++) {
      ctx.save();
      ctx.scale(1 + i / 10, 1 + i / 10);
      ctx.fillStyle = '#F0F';
      ctx.rotate(2 * Math.PI / 6 / i * j);
      ctx.beginPath();
      ctx.arc(20 * i, 0, 4, 0, Math.PI * 2);
      ctx.closePath();
      ctx.fill();
      ctx.restore();
    }
  }
}
// 4
function transform() {
  var cvs = document.getElementById('my-canvas-2');
  var ctx = cvs.getContext('2d');
  cvs.height = 300;
  cvs.width = 300;

  ctx.translate(150, 150);
  for (var i = 1; i < 7; i++) {
    for (var j = 0; j < i * 6; j++) {
      ctx.save();
      ctx.fillStyle = '#F0F';
      ctx.rotate(2 * Math.PI / 6 / i * j);
      ctx.beginPath();
      ctx.arc(20 * i, 0, 4, 0, Math.PI * 2);
      ctx.closePath();
      ctx.fill();
      ctx.restore();
    }
  }
}

代碼地址
Demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 最基本的使用創(chuàng)建一個(gè)畫(huà)布所有的操作都在畫(huà)布的context上面canvas是基于狀態(tài)而不是基于對(duì)象的,比如說(shuō)顏色都...
    親愛(ài)的孟良閱讀 1,740評(píng)論 0 4
  • 書(shū)中代碼示例效果展示:Core HTML5 Canvas Examples 基礎(chǔ)知識(shí) canvas元素 canva...
    szu_bee閱讀 3,044評(píng)論 2 28
  • 1.基本用法要使用canvas元素,必須先設(shè)置其width和height屬性,制定可以繪畫(huà)的區(qū)域大小。出現(xiàn)在開(kāi)始和...
    林中歡歌要找笑語(yǔ)閱讀 552評(píng)論 0 0
  • 變形 在了解變形之前,先了解狀態(tài)。 狀態(tài) canvas 的狀態(tài)就是當(dāng)前畫(huà)面應(yīng)用的所有樣式和變形的一個(gè)快照,用來(lái)操作...
    Sachie閱讀 598評(píng)論 0 0
  • 父親今年八十有六,身體每況愈下,生命的盡頭雖像薄霧朦朦朧朧,可在流動(dòng)之間,已經(jīng)可以窺見(jiàn)它的影子了。我早已萌...
    一泓囈語(yǔ)閱讀 441評(píng)論 0 3

友情鏈接更多精彩內(nèi)容