canvas setTransform()方法
此方法的語法格式和transform()方法是完全一樣的。
關於transform()方法的用法可以參閱canvas transform()一章節。
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> canvas { border: 2px dotted #ccc; margin: 50px; } </style> <script> window.onload = function () { var cvs = document.getElementById("canvas"); var ctx = cvs.getContext("2d"); var angle = Math.PI / 180; ctx.fillStyle = "yellow"; ctx.fillRect(0, 0, 250, 100) ctx.setTransform(Math.cos(30*angle),Math.sin(30*angle),-Math.sin(30*angle),Math.cos(30*angle),30,10); ctx.fillStyle = "red"; ctx.fillRect(0, 0, 250, 100); ctx.setTransform(Math.cos(30*angle),Math.sin(30*angle),-Math.sin(30*angle), Math.cos(30*angle),30,10); ctx.fillStyle = "blue"; ctx.fillRect(0, 0, 250, 100); } </script> </head> <body> <canvas id="canvas" width="400" height="250"></canvas> </body> </html>
在上面的程式碼中,我們並沒有看到紅色的矩形,因為它被藍色矩形所覆蓋。
這就體現出此方法與transform()方法的不同之處(也是唯一的區別),那就是setTransform()方法會重置前面的矩陣,然後再繪製出一個新的矩陣;transform()方法則不會重置前面的矩陣,而是在前面的基礎上繼續進行縮放、唯一或者旋轉。
相關文章
- canvas setTransform()CanvasORM
- canvas isPointInPath()方法Canvas
- canvas closePath()方法詳解Canvas
- canvas arc()方法詳解Canvas
- canvas translate()、scale()和rotate()方法程式碼例項Canvas
- flutter自定義View(CustomPainter) 之 canvas的方法總結FlutterViewAICanvas
- 如何將圖片畫到canvas上?都有哪些方法?Canvas
- canvasCanvas
- WPF C# create canvas and draw ellipse in canvasC#Canvas
- canvas rect()Canvas
- canvas strokeStyleCanvas
- canvas lineTo()Canvas
- canvas strokeRect()Canvas
- canvas createRadialGradient()Canvas
- canvas createLinearGradient()Canvas
- canvas arc()Canvas
- canvas stroke()Canvas
- canvas fill()Canvas
- canvas fillStyleCanvas
- canvas transform()CanvasORM
- 初探canvasCanvas
- canvas closePath()Canvas
- canvas moveTo()Canvas
- canvas beginPath()Canvas
- canvas getContext()CanvasContext
- canvas getImageData()Canvas
- canvas isPointInPath()Canvas
- canvas putImageData()Canvas
- canvas drawImage()Canvas
- canvas clip()Canvas
- canvas arcTo()Canvas
- canvas createPattern()Canvas
- canvas createImageData()Canvas
- canvas restore()CanvasREST
- canvas translate()Canvas
- canvas rotate()Canvas
- canvas scale()Canvas
- canvas measureText()Canvas