Javascript 图像平移/缩放
通过结合一些 CSS 和 Jquery UI/draggable,我创建了平移图像的功能,并且使用一些额外的 JS,您现在可以缩放图像。
我遇到的问题是,如果放大图像的左上角是固定的,正如您所期望的那样。我希望图像保持在中央(基于当前平移),以便图像的中间保持在容器的中间,同时变得更大。
我为此编写了一些代码,但不起作用,我希望我的数学是错误的。有人可以帮忙吗?
我希望它像 this 一样工作。当您滚动到图像时,它会根据当前平移使图像保持居中,而不是从角落缩小。
HTML:
<div id="creator_container" style="position: relative; width: 300px; height: 400px; overflow: hidden;">
<img src="/images/test.gif" class="user_image" width="300" style="cursor: move; position: absolute; left: 0; top: 0;">
</div>
Javascript:
$("#_popup_creator .user_image").bind('mousewheel', function(event, delta) {
zoomPercentage += delta;
$(this).css('width',zoomPercentage+'%');
$(this).css('height',zoomPercentage+'%');
var widthOffset = (($(this).width() - $(this).parent().width()) / 2);
$(this).css('left', $(this).position().left - widthOffset);
});
By combining some CSS and Jquery UI / draggable I have created the ability to pan an image and with a little extra JS you can now zoom the image.
The problem I am having is that, if you zoom in the image's top left corner is fixed, as you would expect. What I would like is for the image to stay central (based on the current pan) so that the middle of the image stays in the middle of the container whilst getting larger.
I have written some code for this but doesn't work, I expect my maths is wrong. Could anyone help?
I want it to work like this does. When you scroll into an image it keeps the image centered based on the current pan rather than zooming out from the corner.
HTML:
<div id="creator_container" style="position: relative; width: 300px; height: 400px; overflow: hidden;">
<img src="/images/test.gif" class="user_image" width="300" style="cursor: move; position: absolute; left: 0; top: 0;">
</div>
Javascript:
$("#_popup_creator .user_image").bind('mousewheel', function(event, delta) {
zoomPercentage += delta;
$(this).css('width',zoomPercentage+'%');
$(this).css('height',zoomPercentage+'%');
var widthOffset = (($(this).width() - $(this).parent().width()) / 2);
$(this).css('left', $(this).position().left - widthOffset);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
长话短说,您需要创建一个变换矩阵,以与图像相同的比例缩放,然后使用该矩阵变换图像的位置。如果这个解释对你来说完全是希腊语,请查找“图像变换”和“矩阵数学”。
尽管这是一种不同的编程语言,但本页的开头是一个非常好的开始资源:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3 /flash/geom/Matrix.html
无论如何,我已经在自己的一些项目中实现了这些方法。这是我写的放大函数,它可以按您想要的方式运行:
请注意命令“new Vector.create()”和“Matrix.I(3)”。这些来自 JavaScript 矢量/矩阵数学库 http://sylvester.jcoglan.com/
然后记下“transformPoint” ()”。这是该 ActionScript 链接中的功能之一(加上 http://wxs.ca/js3d/ 上的提示)我使用 sylvester.js 实现了
我编写的全套函数:
Long story short, you need to make a transform matrix to scale by the same amount as the image and then transform the image's position using that matrix. If that explanation is complete greek to you, look up "image transforms" and "matrix math".
The beginning of this page is a pretty good resource to start with even though it's a different programming language:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/geom/Matrix.html
Anyway, I've implemented those methods in some projects of my own. Here's the zoom in function from something I wrote that functions the way you want:
Note the commands "new Vector.create()" and "Matrix.I(3)". Those come from the JavaScript vector/matrix math library http://sylvester.jcoglan.com/
Then note "transformPoint()". That's one of the functions from that ActionScript link (plus hints on http://wxs.ca/js3d/) that I implemented using sylvester.js
For the full set of functions I wrote: