javascript-拉斐尔路径调整大小并相对于容器移动
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-拉斐尔路径调整大小并相对于容器移动,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2822字,纯文字阅读大概需要5分钟。
内容图文
![javascript-拉斐尔路径调整大小并相对于容器移动](/upload/InfoBanner/zyjiaocheng/692/9aeb6064353749cc93807f085360cc2a.jpg)
我正在尝试缩放/移动使用Raphael api创建的SVG路径.无论容器的大小如何,我都希望路径能够整齐地容纳在容器中.我已经搜索了参考资料,网络,但仍在努力使它起作用.
如果有人能告诉我为什么它不起作用,我将非常高兴.
这个小提琴向您展示了我在做什么:http://jsfiddle.net/tolund/3XPxL/5/
JavaScript的:
var draw = function(size) {
var $container = $("#container").empty();
$container.css("height",size+"px").css("width",size+"px");
var paper = Raphael("container");
var pathStr = "M11.166,23.963L22.359,17.5c1.43-0.824,1.43-2.175,"+
"0-3L11.166,8.037c-1.429-0.826-2.598-0.15-2.598,"+
"1.5v12.926C8.568,24.113,9.737,24.789,11.166,23.963z";
// set the viewbox to same size as container
paper.setViewBox(0, 0, $container.width(), $container.height(), true);
// create the path
var path = paper.path(pathStr)
.attr({ fill: "#000", "stroke-width": 0, "stroke-linejoin": "round", opacity: 1 });
// get the path outline box (may be different size than view box.
var box = path.getBBox();
// move the path as much to the top/left as possible within container
path.transform("t" + 0 + "," + 0);
// get the width/height based on path box relative to paper (same size as container)
var w = (paper.width) / box.width;
var h = (paper.height) / box.height;
// scale the path to the container (use "..." to compound transforms)
path.transform('...S' + w + ',' + h + ',0,0');
}
$(function() {
var currentSize = 30;
draw(currentSize);
$("#smaller").click(function(){
currentSize = currentSize < 10 ? currentSize : currentSize * 0.5;
draw(currentSize);
});
$("#bigger").click(function(){
currentSize = 300 < currentSize ? currentSize : currentSize * 2;
draw(currentSize);
});
});
HTML:
<button id="smaller">-</button>
<button id="bigger">+</button>
<div id="container" style="border: 1px #ddd solid; margin: 30px">
</div>
谢谢,
Torgeir.
解决方法:
我认为您的问题是对Viewbox有用的基本误解.在您的代码中,您尝试设置svg元素的视图框,使其与屏幕的坐标空间匹配,然后转换路径以与该坐标空间匹配.没有技术原因,您不能执行此操作,但是它确实可以将“可缩放”从“可缩放矢量图形”中删除.视图框的整个要点是使向量坐标空间与屏幕之间的平移相对.
因此,解决问题的最佳方法不是转换路径以匹配SVG元素,而是使用视图框让SVG的固有可伸缩性为您完成此任务.
首先,首先要创建路径,以便我们可以使用一个对象.我们现在不在乎视图框是什么.
var pathStr = "..."; // The content of the path and its coordinates are completely immaterial
var path = paper.path(pathStr)
.attr({ fill: "#000", "stroke-width": 0, "stroke-linejoin": "round", opacity: 1 });
现在,我们需要做的就是使用视图框将SVG“聚焦”在我们感兴趣的坐标空间上.
var box = path.getBBox();
var margin = Math.max( box.width, box.height ) * 0.1; // because white space always looks nice ;-)
paper.setViewBox(box.x - margin, box.y - margin, box.width + margin * 2, box.height + margin * 2);
就是这样. SVG(无论大小)将从视图框中指定的内部坐标转换为屏幕上的物理坐标.
这是proof-of-concept的小提琴.
内容总结
以上是互联网集市为您收集整理的javascript-拉斐尔路径调整大小并相对于容器移动全部内容,希望文章能够帮你解决javascript-拉斐尔路径调整大小并相对于容器移动所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。