Canvas et Html5

image.png

HTML5

​  Divided into sections by<head> and <body> as former, and add new tags such as <nav>, <article>,<header> and <footer> etc.

  <!doctype html> should be always the first line of HTML5.

  <canvas>element itself is accessible in DOM, but it's elements's are not accessible to DOM. cause it's work in immediate mode and not have its own objects.

  No longer have to specify the script type in HTML5.

  You can put text within canvas tags, so that it'll displayed if the browser does not support canvas.

<!-- detect support -->
<canvas>Your browser does not support HTML5 canvas</canvas>

  And within javascript use function below to detect support:

function canvasSupport () {
    return !!document.createElement('canvas').getContext; 
    //detect whether the canvas element exist and if it's context exist or null
}
function canvasApp() {
   if (!canvasSupport) {
      return;
  }

}

Canvas

Basic Rectangle Shape


fillRect(x,y,width,height);

  Filled rectangle with position [x,y] for width and height.

strokeRect(x,y,width,height);

  Rectangle outline.

clearRectangle(x,y,width,height);

  Clear the specified area.

  Set up style before use:

context.fillstyle = 'red';
context.strokeStyle = 'black';
context.lineWidth  = 2;

Canvas state


  Canvas will store transformations, current clipping region and current values, and can be save and restored by:

context.save();
context.restore()

Using paths to create lines


beginPath(); // to start a path
    beginPath(); closePath(); // subpath considered closed before outer
closePath(); // to end a path

eg:

 context.strokeStyle  = "black";
 context.lineWidth  = 10;
 context.lineCap  = 'square'; // end style of path
 context.beginPath();
 context.moveTo(20, 0);
 context.lineTo(100, 0);
 context.stroke(); // draw
 context.closePath();

LineCap attributes

  1. butt: defualt, a flat edge that is perpendicular to the end of the line.
  2. round: semicicle edge.
  3. square: rectangle edge.

lineJoin attributes

  1. miter: edge is drawn at the join.
  2. round: round edge drawn at the join.

Advanced path methods


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

  x,y define the center of the circle, startAngle and endAngle are radians, anticlockwise define the direction in boolean value.

eg:

context.arc(100, 100, 20, (Math.PI/180)*0, (Math.PI/180)*360, false);
2. arcTo()
context.arcTo(x1, y1, x2, y2, radius);

  Draw a arc from [x1, y1] to [x2, y2].

3. Bezier curves
context.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y); // cubic mode
context.quadraticCurveTo(cpx, cpy, x, y); // quadratic

Compositing


  Control over the transparency and the layering effects.

  • globalAlpha:

  Default -> 1.0, range[0.0,1.0], must be set before a shape is drawn.

  • globalCompositeOperation:

  How shapes are drawn.

  • copy

  Where they overlap, displays the source and not the destination.

  • destination-atop

  Destination atop the source.

  • destination-in

  Destination in the source.

  • destination-out

  Destination out source.

  • destination-over

  Destination over the source.

  • lighter

  Displays the sum of the source image and destination image.

  • source-atop

  Source atop the destination.

  • source-in

  Source in the destination.

  • source-out

  Source out destination.

  • source-over

  (Default.) Source over destination.

  • xor

  Source xor destination.

eg:

//draw a big box on the screen
context.fillStyle = "black"; //
context.fillRect(10, 10, 200, 200);

//leave globalCompositeOperation as is
//now draw a red square
context.fillStyle = "red";
context.fillRect(1, 1, 50, 50);

//now set it to source-over
context.globalCompositeOperation = "source-over";
//draw a red square next to the other one
context.fillRect(60, 1, 50, 50);      //now set to destination-atop
context.globalCompositeOperation = "destination-atop";
context.fillRect(1, 60, 50, 50);

//now set globalAlpha
context.globalAlpha = .5;

//now set to source-atop
context.globalCompositeOperation = "source-atop";
context.fillRect(60, 60, 50, 50);
image.png

globalCompositeOperation does not work properly any more

Simple canvas transformations


  Nowadays mostly used transformations are scale and rotate:

context.setTransform(1,0,0,1,0,0); // transform matrix (according to the former)
var angleInRadians = 45 * Math.PI / 180;
context.rotate(angleInRadians); // use radians instead of degrees
context.translate(x,y); // reset the origin locations
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原来想的 旅游 重心在游 现在感悟 旅游 重心在旅 在路上 放松身心 在路上 放飞心情 在路上 走进自然 在路上 ...
    燚格格阅读 305评论 0 0
  • 今天恢复進食了,但僅限于米湯,早晨、中午兩頓。早起還是有些乏力,関鍵是口中无味,吐出的唾沫都是粘的。開車上班的路上...
    如心1976阅读 230评论 1 0
  • Time is not enough to do everything I want . Today I went...
    西西冒泡阅读 375评论 0 0
  • 醉卧梦中又是他乡 何恋几多空梦一场 你抚袖 我染唇角芬芳 回眸深邃煎煮药炉滚烫 不说 我是后来居上 青阁素月再舞一...
    咪咖阅读 290评论 0 1