超酷的HTML5 Canvas网络画板教程
在今天的HTML教程里,我们要学习一下如何使用HTML5 Canvas实现一个超酷而又简单的网络画板功能。在这个教程中,我们可以选择笔刷的类型、笔刷的大小以及笔刷的颜色,当然作为画板还需要很多功能,这里只是最基本的绘制功能,大家可以自己实现类似矩形、椭圆等复杂元素的绘制。
demo演示
下面我们来简单地分析一下实现这个HTML5网页画板的原理及代码,代码由HTML以及Javascript组成,主要还是Javascript代码。
HTML代码:
- <div style="width:530px;margin:10px auto">
- <div id="canvasDiv"></div>
- </div>
复制代码
HTML代码非常简单,仅仅是构造了一个canvas容器,我们的画板就在这个地方生成。
Javascript代码:
首先我们通过一组变量来定义画板的样式,以及一些数据的初始化:
- var canvas;
- var context;
- var canvasWidth = 490;
- var canvasHeight = 220;
- var padding = 25;
- var lineWidth = 8;
- var colorPurple = "#cb3594";
- var colorGreen = "#659b41";
- var colorYellow = "#ffcf33";
- var colorBrown = "#986928";
- var outlineImage = new Image();
- var crayonImage = new Image();
- var markerImage = new Image();
- var eraserImage = new Image();
- var crayonBackgroundImage = new Image();
- var markerBackgroundImage = new Image();
- var eraserBackgroundImage = new Image();
- var crayonTextureImage = new Image();
- var clickX = new Array();
- var clickY = new Array();
- var clickColor = new Array();
- var clickTool = new Array();
- var clickSize = new Array();
- var clickDrag = new Array();
- var paint = false;
- var curColor = colorPurple;
- var curTool = "crayon";
- var curSize = "normal";
- var mediumStartX = 18;
- var mediumStartY = 19;
- var mediumImageWidth = 93;
- var mediumImageHeight = 46;
- var drawingAreaX = 111;
- var drawingAreaY = 11;
- var drawingAreaWidth = 267;
- var drawingAreaHeight = 200;
- var toolHotspotStartY = 23;
- var toolHotspotHeight = 38;
- var sizeHotspotStartY = 157;
- var sizeHotspotHeight = 36;
- var sizeHotspotWidthObject = new Object();
- sizeHotspotWidthObject.huge = 39;
- sizeHotspotWidthObject.large = 25;
- sizeHotspotWidthObject.normal = 18;
- sizeHotspotWidthObject.small = 16;
- var totalLoadResources = 8;
- var curLoadResNum = 0;
复制代码
接下来开始准备画布,也就是初始化Canvas对象:
- function prepareCanvas()
- {
- // Create the canvas (Neccessary for IE because it doesn't know what a canvas element is)
- var canvasDiv = document.getElementById('canvasDiv');
- canvas = document.createElement('canvas');
- canvas.setAttribute('width', canvasWidth);
- canvas.setAttribute('height', canvasHeight);
- canvas.setAttribute('id', 'canvas');
- canvasDiv.appendChild(canvas);
- if(typeof G_vmlCanvasManager != 'undefined') {
- canvas = G_vmlCanvasManager.initElement(canvas);
- }
- context = canvas.getContext("2d"); // Grab the 2d canvas context
- // Note: The above code is a workaround for IE 8 and lower. Otherwise we could have used:
- // context = document.getElementById('canvas').getContext("2d");
- // Load images
- // -----------
- crayonImage.onload = function() { resourceLoaded();
- };
- crayonImage.src = "images/crayon-outline.png";
- //context.drawImage(crayonImage, 0, 0, 100, 100);
- markerImage.onload = function() { resourceLoaded();
- };
- markerImage.src = "images/marker-outline.png";
- eraserImage.onload = function() { resourceLoaded();
- };
- eraserImage.src = "images/eraser-outline.png";
- crayonBackgroundImage.onload = function() { resourceLoaded();
- };
- crayonBackgroundImage.src = "images/crayon-background.png";
- markerBackgroundImage.onload = function() { resourceLoaded();
- };
- markerBackgroundImage.src = "images/marker-background.png";
- eraserBackgroundImage.onload = function() { resourceLoaded();
- };
- eraserBackgroundImage.src = "images/eraser-background.png";
- crayonTextureImage.onload = function() { resourceLoaded();
- };
- crayonTextureImage.src = "images/crayon-texture.png";
- outlineImage.onload = function() { resourceLoaded();
- };
- outlineImage.src = "images/watermelon-duck-outline.png";
- // Add mouse events
- // ----------------
- $('#canvas').mousedown(function(e)
- {
- // Mouse down location
- var mou** = e.pageX - this.offsetLeft;
- var mouseY = e.pageY - this.offsetTop;
- if(mou** < drawingAreaX) // Left of the drawing area
- {
- if(mou** > mediumStartX)
- {
- if(mouseY > mediumStartY && mouseY < mediumStartY + mediumImageHeight){
- curColor = colorPurple;
- }else if(mouseY > mediumStartY + mediumImageHeight && mouseY < mediumStartY + mediumImageHeight * 2){
- curColor = colorGreen;
- }else if(mouseY > mediumStartY + mediumImageHeight * 2 && mouseY < mediumStartY + mediumImageHeight * 3){
- curColor = colorYellow;
- }else if(mouseY > mediumStartY + mediumImageHeight * 3 && mouseY < mediumStartY + mediumImageHeight * 4){
- curColor = colorBrown;
- }
- }
- }
- else if(mou** > drawingAreaX + drawingAreaWidth) // Right of the drawing area
- {
- if(mouseY > toolHotspotStartY)
- {
- if(mouseY > sizeHotspotStartY)
- {
- var sizeHotspotStartX = drawingAreaX + drawingAreaWidth;
- if(mouseY < sizeHotspotStartY + sizeHotspotHeight && mou** > sizeHotspotStartX)
- {
- if(mou** < sizeHotspotStartX + sizeHotspotWidthObject.huge){
- curSize = "huge";
- }else if(mou** < sizeHotspotStartX + sizeHotspotWidthObject.large + sizeHotspotWidthObject.huge){
- curSize = "large";
- }else if(mou** < sizeHotspotStartX + sizeHotspotWidthObject.normal + sizeHotspotWidthObject.large + sizeHotspotWidthObject.huge){
- curSize = "normal";
- }else if(mou** < sizeHotspotStartX + sizeHotspotWidthObject.small + sizeHotspotWidthObject.normal + sizeHotspotWidthObject.large + sizeHotspotWidthObject.huge){
- curSize = "small";
- }
- }
- }
- else
- {
- if(mouseY < toolHotspotStartY + toolHotspotHeight){
- curTool = "crayon";
- }else if(mouseY < toolHotspotStartY + toolHotspotHeight * 2){
- curTool = "marker";
- }else if(mouseY < toolHotspotStartY + toolHotspotHeight * 3){
- curTool = "eraser";
- }
- }
- }
- }
- else if(mouseY > drawingAreaY && mouseY < drawingAreaY + drawingAreaHeight)
- {
- // Mouse click location on drawing area
- }
- paint = true;
- addClick(mou**, mouseY, false);
- redraw();
- });
- $('#canvas').mousemove(function(e){
- if(paint==true){
- addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
- redraw();
- }
- });
- $('#canvas').mouseup(function(e){
- paint = false;
- redraw();
- });
- $('#canvas').mouseleave(function(e){
- paint = false;
- });
- }
复制代码
看起来很复杂,前面主要是初始化canvas的背景图片,后面主要是初始化画笔事件,像click、mouseup、mouseleave等鼠标事件。
下面是draw的主要方法:
- function redraw()
- {
- // Make sure required resources are loaded before redrawing
- if(curLoadResNum < totalLoadResources){ return; }
- clearCanvas();
- var locX;
- var locY;
- if(curTool == "crayon")
- {
- // Draw the crayon tool background
- context.drawImage(crayonBackgroundImage, 0, 0, canvasWidth, canvasHeight);
- // Purple
- locX = (curColor == colorPurple) ? 18 : 52;
- locY = 19;
- context.beginPath();
- context.moveTo(locX + 41, locY + 11);
- context.lineTo(locX + 41, locY + 35);
- context.lineTo(locX + 29, locY + 35);
- context.lineTo(locX + 29, locY + 33);
- context.lineTo(locX + 11, locY + 27);
- context.lineTo(locX + 11, locY + 19);
- context.lineTo(locX + 29, locY + 13);
- context.lineTo(locX + 29, locY + 11);
- context.lineTo(locX + 41, locY + 11);
- context.closePath();
- context.fillStyle = colorPurple;
- context.fill();
- if(curColor == colorPurple){
- context.drawImage(crayonImage, locX, locY, mediumImageWidth, mediumImageHeight);
- }else{
- context.drawImage(crayonImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);
- }
- // Green
- locX = (curColor == colorGreen) ? 18 : 52;
- locY += 46;
- context.beginPath();
- context.moveTo(locX + 41, locY + 11);
- context.lineTo(locX + 41, locY + 35);
- context.lineTo(locX + 29, locY + 35);
- context.lineTo(locX + 29, locY + 33);
- context.lineTo(locX + 11, locY + 27);
- context.lineTo(locX + 11, locY + 19);
- context.lineTo(locX + 29, locY + 13);
- context.lineTo(locX + 29, locY + 11);
- context.lineTo(locX + 41, locY + 11);
- context.closePath();
- context.fillStyle = colorGreen;
- context.fill();
- if(curColor == colorGreen){
- context.drawImage(crayonImage, locX, locY, mediumImageWidth, mediumImageHeight);
- }else{
- context.drawImage(crayonImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);
- }
- // Yellow
- locX = (curColor == colorYellow) ? 18 : 52;
- locY += 46;
- context.beginPath();
- context.moveTo(locX + 41, locY + 11);
- context.lineTo(locX + 41, locY + 35);
- context.lineTo(locX + 29, locY + 35);
- context.lineTo(locX + 29, locY + 33);
- context.lineTo(locX + 11, locY + 27);
- context.lineTo(locX + 11, locY + 19);
- context.lineTo(locX + 29, locY + 13);
- context.lineTo(locX + 29, locY + 11);
- context.lineTo(locX + 41, locY + 11);
- context.closePath();
- context.fillStyle = colorYellow;
- context.fill();
- if(curColor == colorYellow){
- context.drawImage(crayonImage, locX, locY, mediumImageWidth, mediumImageHeight);
- }else{
- context.drawImage(crayonImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);
- }
- // Yellow
- locX = (curColor == colorBrown) ? 18 : 52;
- locY += 46;
- context.beginPath();
- context.moveTo(locX + 41, locY + 11);
- context.lineTo(locX + 41, locY + 35);
- context.lineTo(locX + 29, locY + 35);
- context.lineTo(locX + 29, locY + 33);
- context.lineTo(locX + 11, locY + 27);
- context.lineTo(locX + 11, locY + 19);
- context.lineTo(locX + 29, locY + 13);
- context.lineTo(locX + 29, locY + 11);
- context.lineTo(locX + 41, locY + 11);
- context.closePath();
- context.fillStyle = colorBrown;
- context.fill();
- if(curColor == colorBrown){
- context.drawImage(crayonImage, locX, locY, mediumImageWidth, mediumImageHeight);
- }else{
- context.drawImage(crayonImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);
- }
- }
- else if(curTool == "marker")
- {
- // Draw the marker tool background
- context.drawImage(markerBackgroundImage, 0, 0, canvasWidth, canvasHeight);
- // Purple
- locX = (curColor == colorPurple) ? 18 : 52;
- locY = 19;
- context.beginPath();
- context.moveTo(locX + 10, locY + 24);
- context.lineTo(locX + 10, locY + 24);
- context.lineTo(locX + 22, locY + 16);
- context.lineTo(locX + 22, locY + 31);
- context.closePath();
- context.fillStyle = colorPurple;
- context.fill();
- if(curColor == colorPurple){
- context.drawImage(markerImage, locX, locY, mediumImageWidth, mediumImageHeight);
- }else{
- context.drawImage(markerImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);
- }
- // Green
- locX = (curColor == colorGreen) ? 18 : 52;
- locY += 46;
- context.beginPath();
- context.moveTo(locX + 10, locY + 24);
- context.lineTo(locX + 10, locY + 24);
- context.lineTo(locX + 22, locY + 16);
- context.lineTo(locX + 22, locY + 31);
- context.closePath();
- context.fillStyle = colorGreen;
- context.fill();
- if(curColor == colorGreen){
- context.drawImage(markerImage, locX, locY, mediumImageWidth, mediumImageHeight);
- }else{
- context.drawImage(markerImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);
- }
- // Yellow
- locX = (curColor == colorYellow) ? 18 : 52;
- locY += 46;
- context.beginPath();
- context.moveTo(locX + 10, locY + 24);
- context.lineTo(locX + 10, locY + 24);
- context.lineTo(locX + 22, locY + 16);
- context.lineTo(locX + 22, locY + 31);
- context.closePath();
- context.fillStyle = colorYellow;
- context.fill();
- if(curColor == colorYellow){
- context.drawImage(markerImage, locX, locY, mediumImageWidth, mediumImageHeight);
- }else{
- context.drawImage(markerImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);
- }
- // Yellow
- locX = (curColor == colorBrown) ? 18 : 52;
- locY += 46;
- context.beginPath();
- context.moveTo(locX + 10, locY + 24);
- context.lineTo(locX + 10, locY + 24);
- context.lineTo(locX + 22, locY + 16);
- context.lineTo(locX + 22, locY + 31);
- context.closePath();
- context.fillStyle = colorBrown;
- context.fill();
- if(curColor == colorBrown){
- context.drawImage(markerImage, locX, locY, mediumImageWidth, mediumImageHeight);
- }else{
- context.drawImage(markerImage, 0, 0, 59, mediumImageHeight, locX, locY, 59, mediumImageHeight);
- }
- }
- else if(curTool == "eraser")
- {
- context.drawImage(eraserBackgroundImage, 0, 0, canvasWidth, canvasHeight);
- context.drawImage(eraserImage, 18, 19, mediumImageWidth, mediumImageHeight);
- }else{
- alert("Error: Current Tool is undefined");
- }
- if(curSize == "small"){
- locX = 467;
- }else if(curSize == "normal"){
- locX = 450;
- }else if(curSize == "large"){
- locX = 428;
- }else if(curSize == "huge"){
- locX = 399;
- }
- locY = 189;
- context.beginPath();
- context.rect(locX, locY, 2, 12);
- context.closePath();
- context.fillStyle = '#333333';
- context.fill();
- // Keep the drawing in the drawing area
- context.save();
- context.beginPath();
- context.rect(drawingAreaX, drawingAreaY, drawingAreaWidth, drawingAreaHeight);
- context.clip();
- var radius;
- var i = 0;
- for(; i < clickX.length; i++)
- {
- if(clickSize[i] == "small"){
- radius = 2;
- }else if(clickSize[i] == "normal"){
- radius = 5;
- }else if(clickSize[i] == "large"){
- radius = 10;
- }else if(clickSize[i] == "huge"){
- radius = 20;
- }else{
- alert("Error: Radius is zero for click " + i);
- radius = 0;
- }
- context.beginPath();
- if(clickDrag[i] && i){
- context.moveTo(clickX[i-1], clickY[i-1]);
- }else{
- context.moveTo(clickX[i], clickY[i]);
- }
- context.lineTo(clickX[i], clickY[i]);
- context.closePath();
- if(clickTool[i] == "eraser"){
- //context.globalCompositeOperation = "destination-out"; // To erase instead of draw over with white
- context.strokeStyle = 'white';
- }else{
- //context.globalCompositeOperation = "source-over"; // To erase instead of draw over with white
- context.strokeStyle = clickColor[i];
- }
- context.lineJoin = "round";
- context.lineWidth = radius;
- context.stroke();
- }
- //context.globalCompositeOperation = "source-over";// To erase instead of draw over with white
- context.restore();
- // Overlay a crayon texture (if the current tool is crayon)
- if(curTool == "crayon"){
- context.globalAlpha = 0.4; // No IE support
- context.drawImage(crayonTextureImage, 0, 0, canvasWidth, canvasHeight);
- }
- context.globalAlpha = 1; // No IE support
- // Draw the outline image
- context.drawImage(outlineImage, drawingAreaX, drawingAreaY, drawingAreaWidth, drawingAreaHeight);
- }
复制代码
其实HTML5说白了还是需要很多Javascript支持,不过Canvas非常不错,可以让你在上面自由地绘制图形和动画。这款基于HTML5 Canvas的网页画板就是一个很好的例子。
代码下载
超酷的HTML5 Canvas网络画板教程相关推荐
- canvas动画科技园_8个经典炫酷的HTML5 Canvas动画欣赏
HTML5非常强大,尤其是Canvas技术的应用,让HTML5几乎可以完成所有Flash能完成的效果.本文精选了8个经典炫酷的HTML5 Canvas动画欣赏,每一个都提供全部的源代码,希望对你有所帮 ...
- html5字体动画效果,7款超华丽的HTML5 Canvas文字动画特效
原标题:7款超华丽的HTML5 Canvas文字动画特效 文字是网页中最为常见的元素之一,当然我们使用最多的就是调整文字的颜色.大小等基本属性.有时候我们在一些活动页面上需要展示特别样式的文字效果,这 ...
- 好看的html导航栏作品,精选10款超酷的HTML5/CSS3菜单
本文作者html5tricks,转载请注明出处 今天向大家精选了10款超酷的 1.CSS3手风琴菜单 下拉展开带弹性动画 利用CSS3技术可以实现各种各样的网页菜单,我们之前也在CSS3菜单栏目中分享 ...
- 精选10款超酷的HTML5/CSS3菜单
今天向大家精选了10款超酷的HTML5/CSS3菜单,给你的网页添加不一样的精彩,一起来围观一下吧. 1.CSS3手风琴菜单 下拉展开带弹性动画 利用CSS3技术可以实现各种各样的网页菜单,我们之前也 ...
- html5 电子白板 直播,HTML5 canvas 做画板画图 可以做电子白板
HTML5 canvas 做画板画图 可以做电子白板 HTML5 canvas 做画板画图 可以做电子白板 var canvas; var context; var tool; /** * calle ...
- HTML5 canvas 做画板画图 可以做电子白板
HTML5 canvas 做画板画图 可以做电子白板 <html><head><meta charset="utf-8"><title&g ...
- html5超酷单页面,精选10款超酷的HTML5/CSS3菜单
原标题:精选10款超酷的HTML5/CSS3菜单 今天向大家精选了10款超酷的HTML5/CSS3菜单,给你的网页添加不一样的精彩,一起来围观一下吧. 1.CSS3手风琴菜单 下拉展开带弹性动画 利用 ...
- ae制h5文字动画_7款超华丽的HTML5 Canvas文字动画特效
本文作者html5tricks,转载请注明出处 文字是网页中最为常见的元素之一,当然我们使用最多的就是调整文字的颜色.大小等基本属性.有时候我们在一些活动页面上需要展示特别样式的文字效果,这时候我们就 ...
- 超酷震撼 HTML5/CSS3动画应用及源码
HTML5可以制作非常华丽的动画效果,这点通过之前的分享学习我们已经有深刻的了解了,今天我们主要来分享一些HTML5结合CSS3形成的超炫震撼的动画应用以及它们的源代码,真的非常不错. 1.纯CSS3 ...
最新文章
- poj3686(最小权值完美匹配)
- Logback 配置文件这么写,日志 TPS 提高 10 倍
- msql查询指定日期
- oracle hyperion招聘,Hyperion Planning功能顾问
- progress与meter的区别
- 常见的一些 Hash 函数
- linux 获取CPU NUMA内存节点信息
- RealNetworks CTO:我们追求低复杂度的软解码
- P3033 [USACO11NOV]牛的障碍Cow Steeplechase
- 用openbabel将pdbqt文件转成pdb
- 猛龙出击•东莞赛区新闻发布会隆重举行,粤港澳大湾区将迎来首场搏击盛宴...
- VMware虚拟机CentOS7 - VMnet8网络配置及常见问题解决
- 计算机“复制粘贴”之父去世
- 手把手学ROR三——Rails的代码方式
- mysql 联合主键的作用
- 索爱SA-K37拉杆音箱,随时随地帮你撑起一个大舞台
- c语言atm程序个人总结,自己写的ATM模拟程序
- 逆向最大匹配分词RMM
- Banner信息收集和美杜莎使用(9.26 第十二天)
- 技术贴(1):将移动硬盘名从F盘改为E盘
热门文章
- PyTorch随机生成一个布尔型张量
- 交换机短路_交换机端口短路 导致上网不正常
- Atmel Studio 6.0 重新安装
- 每周全球科技十大新闻(2021.6.21-6.27)
- 如何查询一个 app 的 Android 和 iOS 下载量?
- 时间管理类入门书籍分享
- AI 上当,“苹果”变 “iPod”
- 云原生 | 混沌工程工具 ChaosBlade Operator Pod 篇(文末赠书)
- 又找到一款微信机器人!(附源码)
- Java 某天的开始时间与结束时间