日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > HTML >内容正文

HTML

HTML5画布(CANVAS)速查简表

發(fā)布時(shí)間:2025/6/15 HTML 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HTML5画布(CANVAS)速查简表 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
  • >HTML5畫(huà)布(Canvas)元素

    <canvas id="myCanvas" width="500" height="300">

    瀏覽器不支持畫(huà)布(canvas)時(shí)的備案

    <canvas id="myCanvas" width="500" height="300">your browser doesn't support canvas! </canvas>

    2d context

    var context = canvas.getContext('2d');

    Webgl context (3d)

    var context = canvas.getContext('webgl');
  • 圖形

    繪制方形

    context.rect(x, y, width, height); context.fill(); context.stroke();

    填充區(qū)域

    context.fillRect(x, y, width, height);

    繪制方形的邊框

    context.strokeRect(x, y, width, height);

    繪制圓形

    context.arc(x, y, radius, 0, Math.PI * 2); context.fill(); context.stroke();
  • 風(fēng)格

    填充

    context.fillStyle = 'red'; context.fill();

    勾勒

    context.strokeStyle = 'red'; context.stroke();

    線(xiàn)性漸變

    var grd = context.createLinearGradient(x1, y1, x2, y2); grd.addColorStop(0, 'red'); grd.addColorStop(1, 'blue'); context.fillStyle = grd; context.fill();

    徑向漸變

    var grd = context.createRadialGradient(x1, y1, radius1, x2, y2, radius2); grd.addColorStop(0, 'red'); grd.addColorStop(1, 'blue'); context.fillStyle = grd; context.fill();

    圖案

    var imageObj = new Image(); imageObj.onload = function() {var pattern = context.createPattern(imageObj, 'repeat');context.fillStyle = pattern;context.fill(); }; imageObj.src = 'path/to/my/image.jpg';

    交點(diǎn)

    context.lineJoin = 'miter|round|bevel';

    線(xiàn)頭

    context.lineCap = 'butt|round|square';

    陰影

    context.shadowColor = 'black'; context.shadowBlur = 20; context.shadowOffsetX = 10; context.shadowOffsetY = 10;

    Alpha (透明)

    context.globalAlpha = 0.5; // between 0 and 1
  • 顏色格式

    字符串

    context.fillStyle = 'red';

    16進(jìn)制

    context.fillStyle = '#ff0000';

    16進(jìn)制簡(jiǎn)寫(xiě)

    context.fillStyle = '#f00';

    RGB

    context.fillStyle = 'rgb(255,0,0)';

    RGBA

    context.fillStyle = 'rgba(255,0,0,1)';
  • 路徑

    開(kāi)始路徑

    context.beginPath();

    畫(huà)線(xiàn)

    context.lineTo(x, y);

    弧形

    context.arc(x, y, radius, startAngle, endAngle, counterClockwise);

    二次曲線(xiàn)

    context.quadraticCurveTo(cx, cy, x, y);

    二次曲線(xiàn)

    context.bezierCurveTo(cx1, cy1, cx2, cy2, x, y);

    關(guān)閉路徑

    context.closePath();
  • 圖片

    畫(huà)圖

    var imageObj = new Image(); imageObj.onload = function() {context.drawImage(imageObj, x, y); }; imageObj.src = 'path/to/my/image.jpg';

    指定尺寸畫(huà)圖

    var imageObj = new Image(); imageObj.onload = function() {context.drawImage(imageObj, x, y, width, height); }; imageObj.src = 'path/to/my/image.jpg';

    裁剪圖片

    var imageObj = new Image(); imageObj.onload = function() {context.drawImage(imageObj, sx, sy, sw, sh, dx, dy, dw, dh); }; imageObj.src = 'path/to/my/image.jpg';
  • 文本

    寫(xiě)文字

    context.font = '40px Arial'; context.fillStyle = 'red'; context.fillText('Hello World!', x, y);

    寫(xiě)鏤空文字

    context.font = '40pt Arial'; context.strokeStyle = 'red'; context.strokeText('Hello World!', x, y);

    粗體

    context.font = 'bold 40px Arial';

    斜體

    context.font = 'italic 40px Arial';

    對(duì)齊方式

    context.textAlign = 'start|end|left|center|right';

    文字基線(xiàn)

    context.textBaseline = 'top|hanging|middle|alphabetic|ideographic |bottom';

    獲取文本寬度

    var width = context.measureText('Hello world').width;
  • 動(dòng)畫(huà)

    移動(dòng)

    context.translate(x, y);

    擴(kuò)大縮小

    context.scale(x, y);

    旋轉(zhuǎn)

    context.rotate(radians);

    水平翻轉(zhuǎn)

    context.scale(-1, 1);

    上下翻轉(zhuǎn)

    context.scale(1, -1);

    自定義變換

    context.transform(a, b, c, d ,e, f);

    設(shè)置變換

    context.setTransform(a, b, c, d ,e, f);

    切割

    context.transform(1, sy, sx, 1, 0, 0);

    重置

    context.setTransform(1, 0, 0, 1, 0, 0);
  • 狀態(tài)存儲(chǔ)

    存儲(chǔ)

    context.save();

    恢復(fù)

    context.restore();
  • 裁剪

    裁剪

    // draw path here context.clip();
  • 圖像數(shù)據(jù)

    獲取圖像數(shù)據(jù)

    var imageData = context.getImageData(x, y, width, height); var data = imageData.data;

    遍歷像素點(diǎn)

    var imageData = context.getImageData(x, y, width, height); var data = imageData.data; var len = data.length; var i, red, green, blue, alpha;for(i = 0; i < len; i += 4) {red = data[i];green = data[i + 1];blue = data[i + 2];alpha = data[i + 3]; }

    沿坐標(biāo)遍歷像素點(diǎn)

    var imageData = context.getImageData(x, y, width, height); var data = imageData.data; var x, y, red, green, blue, alpha;for(y = 0; y < imageHeight; y++) {for(x = 0; x < imageWidth; x++) {red = data[((imageWidth * y) + x) * 4];green = data[((imageWidth * y) + x) * 4 + 1];blue = data[((imageWidth * y) + x) * 4 + 2];alpha = data[((imageWidth * y) + x) * 4 + 3];} }

    設(shè)置圖像數(shù)據(jù)

    context.putImageData(imageData, x, y);
  • DATA URLS

    獲取Data URL

    var dataURL = canvas.toDataURL();

    使用Data URL生成圖像

    var imageObj = new Image(); imageObj.onload = function() {context.drawImage(imageObj, 0, 0); };imageObj.src = dataURL;
  • 合成

    合成操作

    context.globalCompositeOperation = 'source-atop|source-in|source-out|source-over|destination-atop|destination-in|destination-out|destination-over|lighter|xor|copy';

總結(jié)

以上是生活随笔為你收集整理的HTML5画布(CANVAS)速查简表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。