博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HTML5 Canvas Overview
阅读量:4913 次
发布时间:2019-06-11

本文共 2593 字,大约阅读时间需要 8 分钟。

 

This text provides an overview of the HTML5 canvas basic usage. The overview is split into two parts:

  1. Declaring an HTML5 canvas element.
  2. Drawing graphics on the canvas element.

Declaring a Canvas Element

First, let's see how to declare a canvas element in an HTML page:

HTML5 Canvas not supported

The code above declares a single canvas element with width set to 500, height set to 150, and style set to a 1 pixel border with color #cccccc.

The text inside the canvas element is ignored, if the browser supports the HTML5 canvas element. If the HTML5 canvas element is not supported, the text will probably be displayed as ordinary text by the browser.

You should put the canvas HTML code in your page at the location where you want the canvas to be visible. Just like any other HTML element (e.g. a div element).

Drawing Graphics on a Canvas Element

Graphics drawn on an HTML5 canvas is drawn in immediate mode. Immediate mode means, that as soon as you have drawn a shape on the canvas, the canvas no longer knows anything about that shape. The shape is visible, but you cannot manipulate that shape individually. It is like a real canvas for a painting. Once painted, all you have left is color pigments / pixels.

This is contrary to SVG, where you can manipulate the shapes individually, and have the whole diagram redrawn. In HTML5 you will have to redraw everything yourself, if you want to change the drawn figure.

Drawing graphics on an HTML5 canvas element is done using JavaScript, following these steps:

  1. Wait for the page to be fully loaded.
  2. Obtain a reference to the canvas element.
  3. Obtain a 2D context from the canvas element.
  4. Draw graphics using the draw functions of 2D context.

Here is a simple code example that shows the above steps:

First, an event listener function is attached to the window. This event listener function is executed when the page is loaded. This function calls another function I have defined, called drawExamples().

Second, the drawExamples() function obtains a reference to the canvas element usingdocument.getElementById() function, passing the id of the canvas element, as defined in the declaration of the canvas element.

Third, the drawExamples() function obtains a reference to a 2D context from the canvas element, by callingcanvas.getContext("2d") on the canvas element obtained earlier.

Fourth, the drawExamples() function calls various drawing functions on the 2D context object, which results in graphics being drawn on the canvas.

Here is how the code looks when executed:

 

转载于:https://www.cnblogs.com/hephec/p/4563370.html

你可能感兴趣的文章
python解决上楼梯问题
查看>>
变参宏 __VA_ARGS__
查看>>
sql 语句
查看>>
VUE一 基础语法
查看>>
[MySQl]MySQL忘记密码
查看>>
Android的minSdkVersion,targetSdkVersion,maxSdkVersion
查看>>
Xceed WinForm数据表格控件Xceed Grid For .NET控件详细介绍及下载地址
查看>>
ecos启动流程分析
查看>>
Oracle CASE WHEN 用法介绍
查看>>
linux 下连接mysql服务器
查看>>
DOMContentLoad 首屏渲染
查看>>
rpm检验是否被改动过
查看>>
Sphinx-简介及原理
查看>>
【Linux】深入理解Linux中内存管理
查看>>
WEB 移动网站 手机点击 打电话 发短信
查看>>
2019CSUST集训队选拔赛题解(一)
查看>>
李晓菁201771010114《面向对象程序设计(Java)》第三周学习总结
查看>>
Typedef与Struct
查看>>
Linux常用网络命令整理
查看>>
JMeter学习笔记--使用URL回写来处理用户会话
查看>>