`
rayln
  • 浏览: 424449 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Canvas绘图

 
阅读更多
例子:
<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 &lt; 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 &lt; 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 绘图实现

    HTML5 CANVAS 绘图 JS 只需要简单的一行代码即可渲染整个绘图版 主要用于实现手写签名

    canvas绘图任意矩形、多边形

    &lt;title&gt;canvas绘图 canvas { border: 1px solid black; } &lt;canvas id="myCanvas" width="800" height="600"&gt;&lt;/canvas&gt; &lt;script src="index.js"&gt;&lt;/script&gt; ``` 接着,在`index.js`中,我们初始化...

    canvas绘图 制作的小案例

    canvas绘图 制作的小案例 canvas绘图 制作的小案例 canvas绘图 制作的小案例 canvas绘图 制作的小案例

    基于C#开发的图像绘制、Canvas绘图及图片处理应用开发

    总之,"基于C#开发的图像绘制、Canvas绘图及图片处理应用开发"这个主题涵盖了C#编程中一个非常实用的领域,不仅涉及到基本的图形绘制,还包括了高级的图像处理技巧。掌握这些技能,开发者可以创建出各种富于创意的...

    SVG和Canvas绘图:SVG和Canvas的性能优化技巧.docx

    SVG和Canvas绘图:SVG和Canvas的性能优化技巧.docx

    绚丽的计时效果Canvas绘图与动画基础

    这个“绚丽的计时效果Canvas绘图与动画基础”项目,旨在帮助学习者掌握基于Canvas的计时动画基础知识,通过实践加深对Canvas的理解。 在HTML5中,`&lt;canvas&gt;`元素是一个可绘制图形的画布,通过JavaScript来控制其...

    html5 canvas绘图实例代码集.rar

    html5 canvas绘图实例代码集,比较多的Canvas实例演示Demo,不光只有Canvas,还有其它的一些HTML5应用,对于学习HTML5技术相当有帮助,现在移动设备端已广泛应用HTML5技术。

    基于HTML5 Canvas绘图技术应用.pdf

    Canvas绘图技术具有以下几个优势: 1. 像素级别的位图绘图技术:Canvas具备像素级别的位图绘图技术,可以通过浏览器脚本语言(通常是JavaScript)调用自带的函数(方法)进行图形绘制,实现对图像的像素级别的操作...

    Android Canvas绘图Demo

    本篇将深入探讨`Android Canvas绘图`的相关知识点,包括基本概念、常用方法以及实际应用。 一、Canvas基础 1. `Canvas`对象:在Android中,`Canvas`是绘画的基础,它就像一块画布,我们可以在上面绘制各种元素。...

    原生JS使用Canvas实现拖拽式绘图功能

    原生JS使用Canvas实现拖拽式绘图功能的知识点涵盖了Canvas API的基础应用、面向对象编程思想在Canvas绘图中的运用以及鼠标事件的处理,以下是详细解析: 1. Canvas API基础知识 - Canvas元素:HTML5新增的Canvas...

    12. 画布开发1Canvas绘图2.rar

    画布开发1Canvas绘图2”教程为J2ME开发者提供了一个深入了解和实践Canvas绘图的平台,通过学习这个主题,开发者可以构建出富有创意和视觉吸引力的J2ME应用。结合实际代码示例和SWF演示,学习过程将更加直观和有效。

    SVG和Canvas绘图:SVG和Canvas绘图基础介绍.docx

    SVG和Canvas绘图:SVG和Canvas绘图基础介绍.docx

    Canvas绘图程序_java_

    Canvas绘图程序在Java编程中是一个重要的概念,主要用于在图形用户界面(GUI)上进行动态图形绘制。在Java中,Canvas是AWT(Abstract Window Toolkit)库中的一个类,它是Component类的子类,用于创建自定义的绘图...

    canvas绘图.zip

    这个"canvas绘图.zip"文件很可能是包含了一系列关于Canvas绘图的学习资料或者示例代码。Canvas API允许我们创建丰富的交互式图形,包括动画、图像处理、数据可视化等,广泛应用于网页游戏、图表制作、图像编辑等领域...

    基于HTML5 canvas绘图技术研究.pdf

    "基于HTML5 canvas绘图技术研究" HTML5 是 W3C 推荐的下一代 Web 标准,它带来了许多新的特性和功能,其中 Canvas 元素是 HTML5 中的一个重要组件。Canvas 元素提供了一种使用 JavaScript 编程语言绘制图形的方法,...

    H5的canvas绘图技术共13页.pdf.zip

    HTML5的Canvas绘图技术是Web开发中的一个重要组成部分,它为网页提供了强大的图形绘制能力,使得开发者可以直接在浏览器上进行2D图形的绘制。Canvas通过JavaScript API提供了丰富的接口,允许开发者动态创建和修改...

    HTML5 Canvas绘图示例.rar

    这个“HTML5 Canvas绘图示例.rar”压缩包包含了一个具体的应用实例,教你如何利用Canvas API来画直线、图形以及进行各种图形操作。 首先,Canvas API的核心是`&lt;canvas&gt;`元素,它提供了一个二维渲染上下文,通过...

    基于Taro框架的微信小程序canvas绘图组件

    本主题聚焦于如何利用 Taro 构建一个基于 canvas 的绘图组件,该组件具有丰富的功能,如绘制图形、文字以及生成可分享的图片。 首先,我们需要理解 Taro 的核心概念。Taro 是一套开源的多端开发解决方案,它的主要...

    H5的canvas绘图技术.pdf

    H5的canvas绘图技术

Global site tag (gtag.js) - Google Analytics