Introduction to the basic usage of HTML5 Canvas
本篇文章给大家带来的内容是关于HTML5 Canvas的基本用法介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
canvas 是 HTML5 当中我最喜欢的所有新特性中我最喜欢的一个标签了。因为它太强大了,各种有意思的特效都可以实现。
1. canvas 的基本使用方法
- 它是一个行内块元素
- 默认大小是 300 x 150,不能在 css 里给他设置样式,只能在标签内写它的属性。如 width = 400,height = 300
- 获取画布
var canvas = document。querySelector("canvas")
- 获取画笔(上下文)
var ctx = canvas.getContext('2d')
2. canvas 绘制基本的图形
填充矩形
ctx.fillRect(0,0,100,100)
fill:跟填充有关
Rect: 描绘一个矩形
填充图形设置样式
ctx.fillStyle = 'green'
描边矩形
ctx.strokeRect(100,100,100,100)
描边图形设置样式
ctx.strokeStyle = 'white'
ctx.lineWidth = 100
清除整个画布
ctx.clearRect(0,0,canvas.width,canvas.height)
画线段
ctx.moveTo(100,100)
ctx.lineTo(100,100)
描边
ctx.stroke()
填充
ctx.fill()-
起始点和结束点连接
ctx.closePath()
ctx.save()开头
......
ctx.restore()结尾
3. 画布时钟
使用画布我们可以画一个时钟,包括刻度和时针,每一秒走的刻度可以用 Data 对象通过定时器来时时更新。
var canvas = document.querySelector("canvas"); var ctx = canvas.getContext("2d"); function move() { ctx.save() ctx.translate(300,300) // 初始化一些公共的样式 ctx.lineCap = 'round' ctx.strokeStyle = 'black' ctx.lineWidth = 8 ctx.scale(0.5,0.5) // 画外面的圆 ctx.save(); ctx.beginPath(); ctx.strokeStyle = 'gold'; ctx.arc(0,0,150,0,2*Math.PI); ctx.stroke(); ctx.restore(); // 画里面的刻度 ctx.save() ctx.beginPath(); for (var i=0; i <p>静止的图像如下图。</p><p style="text-align: center;"><span class="img-wrap"><img src="/static/imghw/default1.png" data-src="https://img.php.cn//upload/image/207/367/164/1542961926883406.png" class="lazy" title="1542961926883406.png" alt="Introduction to the basic usage of HTML5 Canvas"></span></p><h4 id="刮刮卡效果">刮刮卡效果</h4><p>使用 canvas 的图形合成的属性可以实现图片合成的效果。具体应用于刮刮卡。<br>globalCompositeOperation属性设置或返回如何将一个源(新的)图像绘制到目标(已有)的图像上<br>源图像 = 您打算放置到画布上的绘图<br>目标图像 = 您已经放置在画布上的绘图</p><p style="max-width:90%"><img src="/static/imghw/default1.png" data-src="https://img.php.cn//upload/image/360/936/462/1542961942862890.png" class="lazy" title="1542961942862890.png" alt="Introduction to the basic usage of HTML5 Canvas"></p><pre class="brush:php;toolbar:false">var canvas = document.querySelector("canvas") var ctx = getCtx() log(ctx) ctx.fillStyle = 'yellow' ctx.fillRect(0,0,400,400) ctx.globalCompositeOperation = 'destination-out'; // 鼠标按下 canvas.onmousedown = function (event) { ctx.beginPath() ctx.arc(event.clientX - canvas.offsetLeft,event.clientY - canvas.offsetTop, 20,0,2*Math.PI) ctx.fill() // 鼠标移动 document.onmousemove = function (event) { ctx.beginPath() ctx.arc(event.clientX - canvas.offsetLeft,event.clientY - canvas.offsetTop, 20,0,2*Math.PI) ctx.fill() } // 鼠标抬起 document.onmouseup = function () { document.onmousemove = document.onmouseup = null } return false }
The above is the detailed content of Introduction to the basic usage of HTML5 Canvas. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
