Terbalikkan, gerakkan, sorot, zum masuk, zum keluar
Kod XML/HTMLSalin kandungan ke papan keratan
- var kanvas = dokumen.getElementById('canvas');
- jika (canvas.getContext) {
-
var konteks = kanvas.getContext('2d');
- // 放大与缩小
- context.beginPath();
-
context.strokeStyle = "#000000";
- context.strokeRect(10,10,150,100);
-
- // 放大3倍
- context.scale(3,3);
- context.beginPath();
-
context.strokeStyle = '#cccccc';
- context.strokeRect(10,10,150,100)
-
- // 缩小
- context.scale(0.5,0.5);
- context.beginPath();
-
context.strokeStyle = '#cccccc';
- context.strokeRect(10,10,150,100)
-
- // 翻转
-
var img = baharu Imej();
-
img.src = 'imej/1.jpg';
-
img.onload = fungsi(){
- context.drawImage(img, 10,10);
- context.scale(1, -1);
- context.drawImage(img, 0, -500);
- }
- // 平移
- context.beginPath();
-
context.strokeStyle = '#000000';
- context.strokeRect(10,101,150,100);
- // x移动 50 y 移动100
- context.translate(50,100);
- context.beginPath();
-
context.strokeStyle = '#cccccc';
- context.strokeRect(10,10,150,100);
- // 旋转
- context.beginPath();
-
context.strokeStyle = '#000000';
- context.strokeRect(200,50,100,50);
- // 默认旋转是根据0,0中心,使用terjemah可以按照自己的设置的中心旋
context.translate(250,75); -
-
context.rotate(45 * Math.PI /180); -
context.translate(-250, -75); -
-
context.beginPath(); -
-
context.strokeStyle = '#cccccc';
context.strokeRect(200,50,100,50); -
-
// mengubah 矩阵 -
context.beginPath(); -
-
context.strokeStyle = '#000000';
context.strokeRect(10,10,150,100); -
-
context.transform(3,0,0,3,0,0); -
context.beginPath(); -
-
context.strokeStyle = '#cccccc';
context.strokeRect(10,10,150,100); -
-
} -
渐变、图像组合效果、颜色翻转
Kod XML/HTML
复制内容到剪贴板
- var kanvas = dokumen.getElementById('canvas'); >
jika (canvas.getContext) { -
var -
konteks = kanvas.getContext('2d');
// Kecerunan lukisan linear
- var
grd-
= konteks.createLinearGradient(0,0,200,100);
// kedudukan mestilah menegak antara 0.1-1.0, menunjukkan kedudukan relatif warna dalam kecerunan, warna mewakili warna
grd.addColorStop(0.1, "#00ff00"); -
grd.addColorStop(0.8, "#ff0000"); -
-
- context.fillStyle
= -
grd
context.fillRect(0,0, 200,100);
// Kecerunan jejari
- var
grd- =
konteks-
.createRadialGradient(100,100,10,100,100,50.>);
grd.addColorStop(0.1, "#00ff00");
grd.addColorStop(0.8, '#ff0000');
- context.fillStyle =
- grd
-
context.fillRect(0,0,200,200);
// Kesan gabungan imej
context.fillStyle
- = '#00ff00'
- ;
context.fillRect(10,10,50,50);
- // Lukisan baharu
//context.globalCompositeOperation = "source-over"
- ;
// Hanya lukis kandungan baharu, padam semua kandungan lain
-
- context.globalCompositeOperation = 'copy';
// Di mana grafik bertindih, nilai warna ditolak untuk menentukan
- context.globalCompositeOperation
- = 'lebih gelap';
// Kandungan yang sudah ada pada kanvas hanya akan dikekalkan apabila ia bertindih dengan grafik lain
- context.globalCompositeOperation =
- 'destination-atop';
// Rujukan http://www.w3school.com.cn/htmldom/prop_canvasrenderingcontext2d_globalcompositeoperation.asp
context.beginPath();
- context.fillStyle
- = '#ff0000';
- context.arc(50,50,30,0, 2 * Math.PI);
- context.fill();
-
- // 颜色翻转
-
var img = neu Image();
-
-
img.src = 'images/1.jpg';
-
img.onload = Funktion(){
- context.drawImage(img, 0,0, 1, 1);
-
var imgData = context.getImageData(0,0, 1,1);
-
var Pixel = imgData.data;
- console.log(pixels);
-
for(var i = 0, n = Pixel.length; i < n; i =4) {
- Pixel[i] = 255 - Pixel[i];
- Pixel[i 1] = 255 - Pixel[i 1];
- Pixel[i 2] = 255 - Pixel[i 2];
- }
- context.putImageData(imgData, 250, 0);
- }
- }