JavaScript、html5、canvas实现图片上画超链接
程序员文章站
2022-04-10 16:13:56
...
本文主要为大家详细介绍了JavaScript 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(); }
相关推荐:
以上就是JavaScript、html5、canvas实现图片上画超链接的详细内容,更多请关注其它相关文章!
推荐阅读
-
将HTML5 Canvas的内容保存为图片借助toDataURL实现
-
html5 Canvas实现图片旋转的示例
-
HTML5 Canvas实现图片缩放、翻转、颜色渐变的代码示例
-
html5 canvas移动浏览器上实现图片压缩上传
-
详解通过HTML5 Canvas实现图片的平移及旋转变化的方法
-
Html5 Canvas实现图片标记、缩放、移动和保存历史状态功能 (附转换公式)
-
html5 canvas实现给图片添加平铺水印
-
纯JavaScript实现HTML5 Canvas六种特效滤镜
-
JavaScript+Canvas实现彩色图片转换成黑白图片的方法分析
-
基于Html5 canvas实现裁剪图片和马赛克功能及又拍云上传图片 功能