专家讲述JavaScript html5 canvas实现图片上画超链接
时间:2022-01-21 00:04:33|来源:网络精心整理
这篇文章主要讲解的是JavaScript html5 canvas实现图片上画超链接,文章内容非常详细,相信一定可以解决你的问题,需要的朋友可以参考下哦
本文实例为大家分享了html5 canvas在图片上画超链接的具体代码,供大家参考,具体内容如下
1. html
<canvas id="canvasFile" style="margin-top:15px;" width="500" height="400"></canvas> <input type="button" id="btnRedo" value="Re-Draw" class="btn btn-warning"/>
2. javascript
var photoW = 400; var photoH = 300; var photo; // logic load image into canvas // ... // e.g. // photo = new Image(); // photo.onload = function() { // draw photo into canvas when ready // ctx.drawImage(photo, 0, 0, photoW, photoH); // }; // load photo into canvas // photo.src = picURL; // canvas highlight var canvas = document.getElementById('canvasFile'), ctx = canvas.getContext('2d'), img = new Image; var btnDone = document.getElementById('btnDone'); var btnRedo = document.getElementById('btnRedo'); ctx.strokeStyle = '#FF0000'; function DrawDot(x, y) { var centerX = x; var centerY = y; var radius = 2; ctx.beginPath(); ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); ctx.fillStyle = 'red'; ctx.fill(); ctx.lineWidth = 2; ctx.strokeStyle = '#FF0000'; ctx.stroke(); } function startDrawing() { ctx.drawImage(img, 0, 0, photoW, photoH); canvas.onmousemove = mousemoving; canvas.onmousedown = mousedownhandle; canvas.onmouseup = mouseuphandle; // ## mobile events //touchstart ? to toggle drawing mode “on” //touchend ? to toggle drawing mode “off” //touchmove ? to track finger position, used in drawing canvas.addEventListener('touchmove', touchmove, false); canvas.addEventListener('touchend', mouseuphandle, false); btnRedo.onclick = function (e) { ctx.clearRect(0, 0, ctx.width, ctx.height); ctx.drawImage(photo, 0, 0, photoW, photoH); savedrawing(); } } function savedrawing(e) { var image = document.getElementById('canvasFile').toDataURL("image/jpeg"); image = image.replace('data:image/jpeg;base64,', ''); $("#imgNric1").val(image); }; function mousemoving(e) { if (drawing) { mousedownhandle(e); } } var drawing = false; function mousedownhandle(e) { drawing = true; var r = canvas.getBoundingClientRect(), x = e.clientX - r.left, y = e.clientY - r.top; DrawDot(x, y); } function mouseuphandle(e) { savedrawing(); e.preventDefault(); drawing = false; } //// mobile touch events function touchmove(e) { if (e.clientX > 800) { mousedownhandle(e); return; } var r = canvas.getBoundingClientRect(), //event.changedTouches[0].pageX + ":" + event.changedTouches[0].pageY; x = e.changedTouches[0].pageX - r.left, y = e.changedTouches[0].pageY - r.top; DrawDot(x, y); e.preventDefault(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持它帮你网。
上一篇:站长阐明dropload.js插件下拉刷新和上拉加载使用详解
本文标题:专家讲述JavaScript html5 canvas实现图片上画超链接
本文地址:https://www.tabangni.com/jsstudy/30386.html
查看更多与“javaScript超链接HTML5Canvas”有关的文章
推荐阅读
- 1专家讲述JavaScript html5 canvas实现图片上画超链接
- 2小编详解echarts实现地图定时切换散点与多图表级联联动详解
- 3顶级人才传授解决使用bootstrap的dropdown部件时报错:error:Bootstrap dropdown require Popper.js问题
- 4技术员阐明[Bootstrap-插件使用]Jcrop+fileinput组合实现头像上传功能实例代码
- 5大神阐明用函数式编程技术编写优美的 JavaScript_ibm
- 6权威技术员教您javascript结合Cookies实现浏览记录历史
- 7专业技术员说明JavaScript 验证浏览器是否支持javascript的方法小结
- 8顶级人才详解JavaScript中常见陷阱小结
- 9小编说明js toFixed()方法的重写实现精度的统一
- 10小白细说javascript数组遍历for与for in区别详解
最近更新
- 01-21专家讲述JavaScript html5 canvas实现图片上画超链接
- 01-21小编详解echarts实现地图定时切换散点与多图表级联联动详解
- 01-21php学习之function的用法
- 01-21PHP封装的Twitter访问类实例
- 01-21顶级人才传授解决使用bootstrap的dropdown部件时报错:error:Bootstrap dropdown require Popper.js问题
- 01-21技术员阐明[Bootstrap-插件使用]Jcrop+fileinput组合实现头像上传功能实例代码
- 01-21MySQL与Oracle的语法区别详细对比
- 01-21技术员指导CSS教程:使用ul进行网页的多列布局
- 01-21大神指导css控制div中元素居中的示例
- 01-21大神阐明用函数式编程技术编写优美的 JavaScript_ibm