<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>canvas 陰影、旋轉(zhuǎn)、縮放、裁剪</title>
<style>
#canvas {
background-color: #f9f9f9;
}
</style>
</head>
<body>
<canvas id="canvas">瀏覽器不支持 canvas </canvas>
</body>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.height = 500;
canvas.width = document.documentElement.clientWidth;
// 陰影:顏色、模糊度、偏移量
ctx.save()
ctx.shadowColor = "orange";
ctx.shadowBlur = 5;
ctx.shadowOffsetX = 5;
ctx.shadowOffsetY = -5;
ctx.font = "50px STheiti, simHei";
ctx.fillText("陰影測試", 10, 50);
ctx.restore()
// 重置陰影
// ctx.shadowColor = "";
// ctx.shadowBlur = 0;
// ctx.shadowOffsetX = 0;
// ctx.shadowOffsetY = 0;
// 旋轉(zhuǎn)(坐標系):ctx.rotate(angle); angle 是弧度,計算公式是 angle = degree * Math.PI / 180
// 默認縮放中心點是Canvas的左上角(0, 0)坐標點,如果希望改變縮放中心點,需要先使用translate()方法進行位移。
ctx.rotate((45 * Math.PI) / 180);
ctx.font = "32px STheiti, simHei";
ctx.fillText("旋轉(zhuǎn)測試 0,0", 10, 50);
ctx.setTransform(1, 0, 0, 1, 0, 0); // 恢復到默認坐標系
// 縮放(坐標系):ctx.scale(x, y); x 是 Canvas坐標系水平縮放的比例。支持小數(shù),如果值是-1,表示水平翻轉(zhuǎn),y是縱向
// 默認縮放中心點是Canvas的左上角(0, 0)坐標點,如果希望改變縮放中心點,需要先使用translate()方法進行位移。
ctx.fillRect(400, 10, 10, 10); // 參照縮放前
ctx.scale(1.5, 2); // 坐標系縮放會影響位置和大小
ctx.fillRect(500, 10, 10, 10);
ctx.setTransform(1, 0, 0, 1, 0, 0); // 恢復到默認坐標系
// 裁剪:ctx.clip();
var img = new Image();
img.src = "./images/bg.jpg";
img.onload = function () {
// 剪裁路徑是三角形
ctx.beginPath();
ctx.moveTo(220, 320);
ctx.lineTo(260, 480);
ctx.lineTo(410, 450);
ctx.closePath();
// 剪裁
ctx.clip();
// 填充圖片
ctx.drawImage(img, 220, 320, 250, 167);
};
</script>
</html>
canvas 陰影、旋轉(zhuǎn)、縮放、裁剪
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 填充和描邊的顏色 fillStyle : 設(shè)置或返回用于填充繪畫的顏色 strokeStyle: 設(shè)置或返回用于筆...
- fillStyle 屬性設(shè)置或返回用于填充繪畫的顏色、漸變或樣式 strokeStyle 屬性設(shè)置或返回用于筆觸的...
- css3新增盒模型陰影box-shadow:[inset] x y blur [spread] color參數(shù)in...
- 參考書目:《HTML5 Canvas核心技術(shù) 圖形、動畫與游戲開發(fā)》 <!DOCTYPE html> 2-...