我想实现图形的缩放功能,因为它在谷歌地图中,也就是说,我希望随着它的比例随着鼠标轮事件的缩放,基于鼠标指针的位置,使图形有更清晰的。
我必须通过使用HTML5和JavaScript来实现它。
有人能引导我做同样的事吗?
我问了一些人,他们说我必须用矢量图来实现它。是这样吗?
发布于 2013-01-24 06:08:18
为了做到这一点,你必须有一个偏移量和缩放比例。然后,无论何时绘图,都必须适当地转换您的绘图。画布使这很容易:
ctx.save();
ctx.translate(offset.x, offset.y);
ctx.scale(scale, scale);
// do your drawing
ctx.restore();然后添加相应鼠标事件的处理程序。对于鼠标轮,这将是事件。每当你收到这样的一个事件,你会想要计算一个新的尺度。例如,可以使用以下方法来完成:
// Assuming 'ticks' is some standardized unit of measurement.
// You'll probably want to scale it differently based on event.deltaMode.
var newScale = scale * Math.pow(1.5, ticks);然后你会想要找出未转换的位置。
// Assuming mousePos.x and mousePos.y are the position, relative to the canvas,
// of the mouse event. To untransform, first undo the offset and then undo the
// scale.
var untransformed = {
x: (mousePos.x - offset.x) / scale,
y: (mousePos.y - offset.y) / scale
};然后,表演一些魔术,通过实验来确定:
offset.x += transformed.x * (scale - newScale);
offset.y += transformed.y * (scale - newScale);然后应用比例变化:
scale = newScale;试试看。 (用CoffeeScript编写,使用点击而不是滚动,但数学是一样的)
https://stackoverflow.com/questions/14494631
复制相似问题