例子:
曲线 圆形画法
以下部分是转载: http://blog.bingo929.com/html-5-canvas-the-basics-html5.html
插入图像
drawImage 方法允许在 canvas 中插入其他图像
( img 和 canvas 元素) 。在 Opera 中可以再 canvas 中绘制 SVG 图形。此方法比较复杂,可以有3个、5个或9个参数:
3个参数:最基本的 drawImage 使用方法。一个参数指定图像位置,另两个参数设置图像在 canvas中的位置。
5个参数:中级的 drawImage 使用方法,包括上面所述3个参数,加两个参数指明插入图像宽度和高度 (如果你想改变图像大小)。
9个参数:最复杂drawImage 杂使用方法,包含上述5个参数外,另外4个参数设置源图像中的位置和高度宽度。这些参数允许你在显示图像前动态裁剪源图像。
http://www.robodesign.ro/coding/canvas-primer/20081208/example-drawimage.html
像素级操作
2D Context API 提供了三个方法用于像素级操作:createImageData, getImageData, 和
putImageData。
ImageData对象保存了图像像素值。每个对象有三个属性: width, height 和
data。data 属性类型为CanvasPixelArray,用于储存width*height*4个像素值。每一个像素有RGB值和透明度alpha值(其值为 0 至 255,包括alpha在内!)。像素的顺序从左至右,从上到下,按行存储。
http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata2.html
注意: 不是所有浏览器都实现了 createImageData。在支持的浏览器中,需要通过 getImageData 方法获取 ImageData 对象。请参考示例代码。 http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata2.html
通过 ImageData 可以完成很多功能。如可以实现图像滤镜,或可以实现数学可视化 (如分形和其他特效)。下面特效实现了简单的颜色反转滤镜: http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata.html
文字
虽然最近的 WebKit 版本和 Firefox 3.1 nightly build 才开始支持 Text API ,为了保证文章完整性我决定仍在这里介绍文字 API 。
context 对象可以设置以下 text 属性:
font:文字字体,同 CSS font-family 属性
textAlign:文字水平对齐方式。可取属性值: start, end, left,right, center。默认值:start.
textBaseline:文字竖直对齐方式。可取属性值:top, hanging, middle,alphabetic, ideographic, bottom。默认值:alphabetic.
有两个方法可以绘制文字: fillText 和 strokeText。第一个绘制带 fillStyle 填充的文字,后者绘制只有 strokeStyle 边框的文字。两者的参数相同:要绘制的文字和文字的位置(x,y) 坐标。还有一个可选选项——最大宽度。如果需要的话,浏览器会缩减文字以让它适应指定宽度。
文字对齐属性影响文字与设置的
(x,y) 坐标的相对位置。
http://www.robodesign.ro/coding/canvas-primer/20081208/example-text.html
阴影
shadowColor:阴影颜色。其值和 CSS 颜色值一致。
shadowBlur:设置阴影模糊程度。此值越大,阴影越模糊。其效果和 Photoshop 的高斯模糊滤镜相同。
shadowOffsetX 和 shadowOffsetY:阴影的 x 和 y 偏移量,单位是像素。
http://www.robodesign.ro/coding/canvas-primer/20081208/example-shadows.html
颜色渐变
除了 CSS 颜色, fillStyle 和 strokeStyle 属性可以设置为 CanvasGradient 对象。——通过 CanvasGradient可以为线条和填充使用颜色渐变。
欲创建 CanvasGradient 对象,可以使用两个方法:createLinearGradient 和 createRadialGradient。前者创建线性颜色渐变,后者创建圆形颜色渐变。
创建颜色渐变对象后,可以使用对象的 addColorStop 方法添加颜色中间值。
下面的代码演示了颜色渐变使用方法:
http://www.robodesign.ro/coding/canvas-primer/20081208/example-gradients.html
<script>
function draw(){
var canvas = document.getElementById("can");
var context = canvas.getContext("2d");
//保存绘画状态
context.save();
//移动起点位置
context.translate(70,140);
//原点为起点绘制线段
context.beginPath();
context.moveTo(0,0);
context.lineTo(70,-70);
context.stroke();
//恢复原有绘图状态
context.restore();
}
window.addEventListener("load",draw,true);
</script>
<body>
<canvas id="can" style="border:1px solid;" width="200" height="200"></canvas>
</body>
曲线 圆形画法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Canvas Primer - Example: Using paths</title>
<script type="text/javascript"><!--
window.addEventListener('load', function () {
// Get the canvas element.
var elem = document.getElementById('myCanvas');
if (!elem || !elem.getContext) {
return;
}
// Get the canvas 2d context.
var context = elem.getContext('2d');
if (!context) {
return;
}
context.fillStyle = '#00f';
context.strokeStyle = '#f00';
context.lineWidth = 4;
// Draw a line, then a Bèzier curve.
context.beginPath();
context.moveTo(10, 10);
context.lineTo(100, 100);
context.moveTo(150, 100);
// Arguments: cp1x, cp1y, cp2x, cp2y, x, y
// cp = control point.
context.bezierCurveTo(180, 30, 250, 180, 300, 100);
context.stroke();
context.closePath();
// Draw a circle using the arc function.
context.beginPath();
// Arguments: x, y, radius, start angle, end angle, anticlockwise
context.arc(125, 115, 30, 0, 360, false);
context.stroke();
context.closePath();
}, false);
// --></script>
</head>
<body>
<p><canvas id="myCanvas" width="300" height="150">Your browser does not have
support for Canvas.</canvas></p>
</body>
</html>
以下部分是转载: http://blog.bingo929.com/html-5-canvas-the-basics-html5.html
插入图像
drawImage 方法允许在 canvas 中插入其他图像
( img 和 canvas 元素) 。在 Opera 中可以再 canvas 中绘制 SVG 图形。此方法比较复杂,可以有3个、5个或9个参数:
3个参数:最基本的 drawImage 使用方法。一个参数指定图像位置,另两个参数设置图像在 canvas中的位置。
5个参数:中级的 drawImage 使用方法,包括上面所述3个参数,加两个参数指明插入图像宽度和高度 (如果你想改变图像大小)。
9个参数:最复杂drawImage 杂使用方法,包含上述5个参数外,另外4个参数设置源图像中的位置和高度宽度。这些参数允许你在显示图像前动态裁剪源图像。
http://www.robodesign.ro/coding/canvas-primer/20081208/example-drawimage.html
// Three arguments: the element, destination (x,y) coordinates. context.drawImage(img_elem, dx, dy); // Five arguments: the element, destination (x,y) coordinates, and destination // width and height (if you want to resize the source image). context.drawImage(img_elem, dx, dy, dw, dh); // Nine arguments: the element, source (x,y) coordinates, source width and // height (for cropping), destination (x,y) coordinates, and destination width // and height (resize). context.drawImage(img_elem, sx, sy, sw, sh, dx, dy, dw, dh);
像素级操作
2D Context API 提供了三个方法用于像素级操作:createImageData, getImageData, 和
putImageData。
ImageData对象保存了图像像素值。每个对象有三个属性: width, height 和
data。data 属性类型为CanvasPixelArray,用于储存width*height*4个像素值。每一个像素有RGB值和透明度alpha值(其值为 0 至 255,包括alpha在内!)。像素的顺序从左至右,从上到下,按行存储。
http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata2.html
// Create an ImageData object.
var imgd = context.createImageData(50,50);
var pix = imgd.data;
// Loop over each pixel 和 set a transparent red.
for (var i = 0; n = pix.length, i < n; i += 4) {
pix[i ] = 255; // red channel
pix[i+3] = 127; // alpha channel
}
// Draw the ImageData object at the given (x,y) coordinates.
context.putImageData(imgd, 0,0);
注意: 不是所有浏览器都实现了 createImageData。在支持的浏览器中,需要通过 getImageData 方法获取 ImageData 对象。请参考示例代码。 http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata2.html
通过 ImageData 可以完成很多功能。如可以实现图像滤镜,或可以实现数学可视化 (如分形和其他特效)。下面特效实现了简单的颜色反转滤镜: http://www.robodesign.ro/coding/canvas-primer/20081208/example-imagedata.html
// Get the CanvasPixelArray from the given coordinates and dimensions.
var imgd = context.getImageData(<var>x</var>, <var>y</var>, <var>width</var>, <var>height</var>);
var pix = imgd.data;
// Loop over each pixel and invert the color.
for (var i = 0, n = pix.length; i < n; i += 4) {
pix[i ] = 255 - pix[i ]; // red
pix[i+1] = 255 - pix[i+1]; // green
pix[i+2] = 255 - pix[i+2]; // blue
// i+3 is alpha (the fourth element)
}
// Draw the ImageData at the given (x,y) coordinates.
context.putImageData(imgd, <var>x</var>, <var>y</var>);
文字
虽然最近的 WebKit 版本和 Firefox 3.1 nightly build 才开始支持 Text API ,为了保证文章完整性我决定仍在这里介绍文字 API 。
context 对象可以设置以下 text 属性:
font:文字字体,同 CSS font-family 属性
textAlign:文字水平对齐方式。可取属性值: start, end, left,right, center。默认值:start.
textBaseline:文字竖直对齐方式。可取属性值:top, hanging, middle,alphabetic, ideographic, bottom。默认值:alphabetic.
有两个方法可以绘制文字: fillText 和 strokeText。第一个绘制带 fillStyle 填充的文字,后者绘制只有 strokeStyle 边框的文字。两者的参数相同:要绘制的文字和文字的位置(x,y) 坐标。还有一个可选选项——最大宽度。如果需要的话,浏览器会缩减文字以让它适应指定宽度。
文字对齐属性影响文字与设置的
(x,y) 坐标的相对位置。
http://www.robodesign.ro/coding/canvas-primer/20081208/example-text.html
context.fillStyle = '#00f';
context.font = 'italic 30px sans-serif';
context.textBaseline = 'top';
context.fillText ('Hello world!', 0, 0);
context.font = 'bold 30px sans-serif';
context.strokeText('Hello world!', 0, 50);
阴影
shadowColor:阴影颜色。其值和 CSS 颜色值一致。
shadowBlur:设置阴影模糊程度。此值越大,阴影越模糊。其效果和 Photoshop 的高斯模糊滤镜相同。
shadowOffsetX 和 shadowOffsetY:阴影的 x 和 y 偏移量,单位是像素。
http://www.robodesign.ro/coding/canvas-primer/20081208/example-shadows.html
context.shadowOffsetX = 5; context.shadowOffsetY = 5; context.shadowBlur = 4; context.shadowColor = 'rgba(255, 0, 0, 0.5)'; context.fillStyle = '#00f'; context.fillRect(20, 20, 150, 100);
颜色渐变
除了 CSS 颜色, fillStyle 和 strokeStyle 属性可以设置为 CanvasGradient 对象。——通过 CanvasGradient可以为线条和填充使用颜色渐变。
欲创建 CanvasGradient 对象,可以使用两个方法:createLinearGradient 和 createRadialGradient。前者创建线性颜色渐变,后者创建圆形颜色渐变。
创建颜色渐变对象后,可以使用对象的 addColorStop 方法添加颜色中间值。
下面的代码演示了颜色渐变使用方法:
http://www.robodesign.ro/coding/canvas-primer/20081208/example-gradients.html
// You need to provide the source 和 destination (x,y) coordinates // for the gradient (from where it starts 和 where it ends). var gradient1 = context.createLinearGradient(<var>sx</var>, <var>sy</var>, <var>dx</var>, <var>dy</var>); // Now you can add colors in your gradient. // The first argument tells the position for the color in your gradient. The // accepted value range is from 0 (gradient start) to 1 (gradient end). // The second argument tells the color you want, using the CSS color format. gradient1.addColorStop(0, '#f00'); // red gradient1.addColorStop(0.5, '#ff0'); // yellow gradient1.addColorStop(1, '#00f'); // blue // For the radial gradient you also need to provide source // 和 destination circle radius. // The (x,y) coordinates define the circle center points (start 和 // destination). var gradient2 = context.createRadialGradient(<var>sx</var>, <var>sy</var>, <var>sr</var>, <var>dx</var>, <var>dy</var>, <var>dr</var>); // Adding colors to a radial gradient is the same as adding colors to linear // gradients.
本文详细介绍了HTML5 Canvas的绘图功能,包括基本的绘图操作、路径绘制、图像处理、文字绘制等,并提供了丰富的示例代码。

1万+

被折叠的 条评论
为什么被折叠?



