放大canvas鼠标光标

我正在编写一个HTML5 <canvas>项目,其中涉及使用滚轮放大和缩小图像。 我想像google maps一样放大光标,但是我完全失去了如何计算动作。

我有什么:图像x和y(左上angular); 图像的宽度和高度; 光标x和y相对于canvas的中心。

简而言之,您想要通过偏移量来translate()canvas上下文,使用scale()来放大或缩小canvas上下文,然后再通过鼠标偏移量的相反方向translate() 。 请注意,您需要将光标位置从屏幕空间转换为已转换的canvas上下文。

 ctx.translate(pt.x,pt.y); ctx.scale(factor,factor); ctx.translate(-pt.x,-pt.y); 

演示: http : //phrogz.net/tmp/canvas_zoom_to_cursor.html

我已经在我的网站上提供了一个完整的工作示例 ,供您检查,支持拖动,单击放大,按住Shift键单击,或向上/向下滚动鼠标滚轮。

唯一的(当前)问题是,与Chrome或Firefox相比, Safari变得速度过快 。

我希望,这些JS库会帮助你:(HTML5,JS)

  1. 放大镜

http://www.netzgesta.de/loupe/

  1. CanvasZoom

https://github.com/akademy/CanvasZoom

  1. 滚轮

https://github.com/zynga/scroller

至于我,我正在使用放大镜。 这很棒! 为你最好的情况 – 滚动。

我把@ Phrogz的答案作为基础,并创build了一个小图书馆,使拖放,缩放和旋转的canvas。 这里是例子。

 var canvas = document.getElementById('canvas') //assuming that @param draw is a function where you do your main drawing. var control = new CanvasManipulation(canvas, draw) control.init() control.layout() //now you can drag, zoom and rotate in canvas 

您可以在项目页面上find更详细的示例和文档

我最近需要归档Phrogz已经完成的结果,而不是使用context.scale() ,我根据比率计算每个对象的大小。

这是我想出来的。 背后的逻辑非常简单。 在缩放之前,我计算以百分比表示的边缘点距离,然后将视口调整到正确的位置。

我花了一段时间才想出来,希望能节省一些时间。

 $(function () { var canvas = $('canvas.main').get(0) var canvasContext = canvas.getContext('2d') var ratio = 1 var vpx = 0 var vpy = 0 var vpw = window.innerWidth var vph = window.innerHeight var orig_width = 4000 var orig_height = 4000 var width = 4000 var height = 4000 $(window).on('resize', function () { $(canvas).prop({ width: window.innerWidth, height: window.innerHeight, }) }).trigger('resize') $(canvas).on('wheel', function (ev) { ev.preventDefault() // for stackoverflow var step if (ev.originalEvent.wheelDelta) { step = (ev.originalEvent.wheelDelta > 0) ? 0.05 : -0.05 } if (ev.originalEvent.deltaY) { step = (ev.originalEvent.deltaY > 0) ? 0.05 : -0.05 } if (!step) return false // yea.. var new_ratio = ratio + step var min_ratio = Math.max(vpw / orig_width, vph / orig_height) var max_ratio = 3.0 if (new_ratio < min_ratio) { new_ratio = min_ratio } if (new_ratio > max_ratio) { new_ratio = max_ratio } // zoom center point var targetX = ev.originalEvent.clientX || (vpw / 2) var targetY = ev.originalEvent.clientY || (vph / 2) // percentages from side var pX = ((vpx * -1) + targetX) * 100 / width var pY = ((vpy * -1) + targetY) * 100 / height // update ratio and dimentsions ratio = new_ratio width = orig_width * new_ratio height = orig_height * new_ratio // translate view back to center point var x = ((width * pX / 100) - targetX) var y = ((height * pY / 100) - targetY) // don't let viewport go over edges if (x < 0) { x = 0 } if (x + vpw > width) { x = width - vpw } if (y < 0) { y = 0 } if (y + vph > height) { y = height - vph } vpx = x * -1 vpy = y * -1 }) var is_down, is_drag, last_drag $(canvas).on('mousedown', function (ev) { is_down = true is_drag = false last_drag = { x: ev.clientX, y: ev.clientY } }) $(canvas).on('mousemove', function (ev) { is_drag = true if (is_down) { var x = vpx - (last_drag.x - ev.clientX) var y = vpy - (last_drag.y - ev.clientY) if (x <= 0 && vpw < x + width) { vpx = x } if (y <= 0 && vph < y + height) { vpy = y } last_drag = { x: ev.clientX, y: ev.clientY } } }) $(canvas).on('mouseup', function (ev) { is_down = false last_drag = null var was_click = !is_drag is_drag = false if (was_click) { } }) $(canvas).css({ position: 'absolute', top: 0, left: 0 }).appendTo(document.body) function animate () { window.requestAnimationFrame(animate) canvasContext.clearRect(0, 0, canvas.width, canvas.height) canvasContext.lineWidth = 1 canvasContext.strokeStyle = '#ccc' var step = 100 * ratio for (var x = vpx; x < width + vpx; x += step) { canvasContext.beginPath() canvasContext.moveTo(x, vpy) canvasContext.lineTo(x, vpy + height) canvasContext.stroke() } for (var y = vpy; y < height + vpy; y += step) { canvasContext.beginPath() canvasContext.moveTo(vpx, y) canvasContext.lineTo(vpx + width, y) canvasContext.stroke() } canvasContext.strokeRect(vpx, vpy, width, height) canvasContext.beginPath() canvasContext.moveTo(vpx, vpy) canvasContext.lineTo(vpx + width, vpy + height) canvasContext.stroke() canvasContext.beginPath() canvasContext.moveTo(vpx + width, vpy) canvasContext.lineTo(vpx, vpy + height) canvasContext.stroke() canvasContext.restore() } animate() }) 
 <!DOCTYPE html> <html> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head> <body> <canvas class="main"></canvas> </body> </html>