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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容

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