1. Canvas tag:
1. The HTML5
2. The
3. There are many ways to draw paths, boxes, circles, characters and add images through Canvas.
2. Draw graphics with Canvas
1. Draw a rectangle
<br>
2. Draw a circle
## 3. moveTo and lineTo
4. Use bezierCurveTo to draw the Bezier curve
## 6. Draw a radial gradient
## 7. Draw deformed graphics
8. Draw graphics and synthesize globablCompositeOperation properties
Use different The globalCompositeOperation value draws the rectangle. The orange rectangle is the target image. The pink rectangle is the source image.
Definition and Usage
The globalCompositeOperation property sets or returns how to draw a source (new) image onto the target (existing) image.Source image = the drawing you intend to place on the canvas.
Target Image = The drawing you have placed on the canvas.
Attribute value:
值 | 属性 |
source-atop | <br>在先绘制的图形顶部显示后绘制的图形。后绘制的图形位于先绘制的图形之外的部分是不可见的。 |
source-in | 只绘制相交部分,由后绘制图形的填充覆盖,其余部分透明。 |
source-out | 只绘制后绘制图形不相交的部分,由后绘制图形的填充覆盖,其余部分透明。 |
source-over | 在先绘制的图形上显示后绘制的图形。相交部分由后绘制的图形填充(颜色,渐变,纹理)覆盖 |
destination-atop | 在后绘制的图形顶部显示先绘制的图形。源图像之外的目标图像部分不会被显示。 |
destination-in | 在后绘制的图形中显示先绘制的图形。只绘制相交部分,由先绘制图形的填充覆盖,其余部分透明 |
destination-out | 只有后绘制的图形外的目标图像部分会被显示,源图像是透明的。 |
destination-over | 相交部分由先绘制图形的填充(颜色,渐变,纹理)覆盖. |
lighter | 相交部分由根据先后图形填充来增加亮度。 |
copy | <br>显示后绘制的图形。只绘制后绘制图形。 |
xor | 相交部分透明 |
<br>
以上效果图的代码如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="../js/jquery-1.12.4.min.js"></script> <script> $(function(){ var options = new Array( "source-atop", "source-in", "source-out", "source-over", "destination-atop", "destination-in", "destination-out", "destination-over", "lighter", "copy", "xor" ); var str=""; for(var i=0;i<options.length;i++){ str = "<div id='p_"+i+"' style='float:left'>"+options[i]+"<br/> <canvas id='canvas"+i+"' width='120px' height='100px' style='border:1px solid #ccc;margin:10px 2px 20px;'> </canvas></div>"; $("body").append(str); var cas = document.getElementById('canvas'+i); var ctx = cas.getContext('2d'); ctx.fillStyle = "orange"; ctx.fillRect(10,10,50,50); ctx.globalCompositeOperation = options[i]; ctx.beginPath(); ctx.fillStyle = "pink"; ctx.arc(50,50,30,0,2*Math.PI); ctx.fill(); } }) </script> </head> <body></body> </html>
图形合成
9、给图形绘制阴影
代码如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas基础api</title> <style> canvas{ border:1px solid #ccc; margin:50px; } </style> <script src="../js/jquery-1.12.4.min.js"></script> <script> $(function(){ //获取标签 var cas = document.getElementById('canvas'); //获取绘制环境 var ctx = cas.getContext('2d'); ctx.fillStyle ="#eef"; ctx.fillRect(0,0,300,300); ctx.shadowOffsetX = 10; ctx.shadowOffsetY = 10; ctx.shadowColor = "rgba(100,100,100,0.5)"; ctx.shadowBlur = 7; for(var j=0;j<3;j++){ ctx.translate(80,80); create5star(ctx); ctx.fill(); } function create5star(ctx){ var dx =0; var dy=0; var s=50; ctx.beginPath(); ctx.fillStyle ='rgba(255,0,0,0.5)'; var x =Math.sin(0); var y =Math.cos(0); var dig = Math.PI/5*4; for(var i=0;i<5;i++){ x=Math.sin(i*dig); y=Math.cos(i*dig); ctx.lineTo(dx+x*s,dy+y*s) } ctx.closePath(); ctx.fill(); } }) </script> </head> <body> <canvas id="canvas" width="300" height="300">您的浏览器不支持canvas</canvas> </body> </html>
五角星阴影
10、canvas使用图像
语法:ctx.drawImage(imgobj,left,top,width,height)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas基础api</title> <style> canvas{ border:1px solid #ccc; } </style> <script src="../js/jquery-1.12.4.min.js"></script> <script> $(function(){ //获取标签 var cas = document.getElementById('canvas'); //获取绘制环境 var ctx = cas.getContext('2d'); //导入图片 var img = new Image(); img.src="../images/002.png"; //图片加载完之后,再开始绘制图片 img.onload = function(){ //绘制图片ctx.drawImage(imgobj,left,top,width,height) ctx.drawImage(img,100,50,300,200) } }) </script> </head> <body> <canvas id="canvas" width="500" height="300">您的浏览器不支持canvas</canvas> </body> </html>
The above is the detailed content of HTML5 Canvas to draw graphics. For more information, please follow other related articles on the PHP Chinese website!

Vue和Canvas:如何实现手写签名和手势识别功能引言:手写签名和手势识别功能在现代应用程序中越来越常见,它们可以为用户提供更加直观和自然的交互方式。Vue.js作为一款流行的前端框架,搭配Canvas元素可以实现这两个功能。本文将介绍如何使用Vue.js和Canvas元素来实现手写签名和手势识别功能,并给出相应的代码示例。一、手写签名功能实现要实现手写签

canvas的优势有强大的绘图功能、高性能、跨平台兼容性、支持多种图形格式、可以与其他Web技术集成、可以实现动态效果和可以实现复杂的图像处理。详细介绍:1、Canvas提供了丰富的绘图功能,可以绘制各种形状、线条、文本、图像等;2、Canvas在浏览器中直接操作像素,因此具有很高的性能;3、Canvas是基于HTML5标准的一部分,可以在各种现代浏览器上运行等等。

如何利用Vue和Canvas创建逼真的天气动态背景引言:在现代网页设计中,动态背景效果是吸引用户眼球的重要元素之一。本文将介绍如何利用Vue和Canvas技术来创建一个逼真的天气动态背景效果。通过代码示例,你将学习如何编写Vue组件和利用Canvas绘制不同天气场景,从而实现一个独特而吸引人的背景效果。步骤一:创建Vue项目首先,我们需要创建一个Vue项目。

canvas特效有粒子效果、线条动画、图片处理、文字动画、音频可视化、3D效果、游戏开发等。详细介绍:1、粒子效果,通过控制粒子的位置、速度和颜色等属性来实现各种效果,如烟花、雨滴、星空等;2、线条动画,通过在画布上绘制连续的线条,创建出各种动态的线条效果;3、图片处理,通过对图片进行处理,可以实现各种炫酷的效果,如图片切换、图片特效等;4、文字动画等等特性。

canvas插件有Fabric.js、EaselJS、Konva.js、Three.js、Paper.js、Chart.js和Phaser。详细介绍:1、Fabric.js 是一个基于Canvas的开源 JavaScript 库,它提供了一些强大的功能;2、EaselJS是CreateJS库中的一个模块,它提供了一套简化了Canvas编程的API;3、Konva.js等等。

canvas引擎有Three.js、Pixi.js、EaselJS、Konva.js、Paper.js等。详细介绍:1、Pixi.js,提供了简单易用的API,支持精灵、纹理、滤镜等功能,同时还提供了丰富的工具和插件,方便开发者进行交互、动画和优化等操作;2、Pixi.js,提供了简单易用的API,支持精灵、纹理、滤镜等功能,还提供了丰富的工具和插件;3、EaselJS等等。

Vue和Canvas:如何实现图片的马赛克效果引言:随着Web技术的不断发展,越来越多的人开始使用Vue框架来构建交互式的前端应用。而在前端开发中,常常需要为用户提供图片处理的功能。本文将介绍如何利用Vue和Canvas实现图片的马赛克效果,为用户带来更好的视觉体验。一、马赛克效果概述马赛克效果是一种将图像的细节部分进行像素化处理,使得整个图像变得模糊和抽象

Vue和Canvas:如何实现视频播放器的定制化界面引言:在现代互联网时代,视频已经成为人们生活中必不可少的一部分。为了提供良好的用户体验,许多网站和应用程序都提供了自定义的视频播放器界面。本文将介绍如何使用Vue和Canvas技术实现一个定制化的视频播放器界面。一、前期准备在开始之前,您需要确保您已经安装了Vue和Canvas,并且熟悉这两种技术的基本用法


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
