- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试缩放/移动使用 Raphael api 创建的 SVG 路径。我希望路径能够整齐地放入容器中,无论容器的大小如何。我已经搜索了引用资料和网络,但我仍在努力让它发挥作用。
如果有人能告诉我为什么这不起作用,我会很高兴。
这个 fiddle 向您展示了我在做什么: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>
谢谢,托吉尔。
最佳答案
我认为您的问题是对 View 框的用途存在根本性的误解。在您的代码中,您试图设置 svg 元素的视框,使其与屏幕的坐标空间匹配,然后转换路径以匹配该坐标空间。没有技术原因您不能这样做,但它确实有效地从“可缩放矢量图形”中去除了“可缩放”。 viewbox 的全部意义在于使矢量坐标空间和屏幕之间的平移相对。
那么,解决您的问题的最佳方法不是转换路径以匹配 SVG 元素,而是使用 View 框让 SVG 的内在可伸缩性为您完成此操作。
首先要做的事情是:创建您的路径,以便我们有一个可以使用的对象。此时我们不关心 View 框是什么。
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 });
现在我们需要做的就是使用 View 框将 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(无论大小)将从 View 框中指定的内部坐标转换为其在屏幕上的物理坐标。
这是你的 fiddle 的 fork proof-of-concept .
关于javascript - Raphael 路径调整大小并相对于容器移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21140786/
使用 Raphael,我可以使用以下代码获取对元素的 DOM 对象的引用: element.node 如何获取链接到 DOM 对象的元素?换句话说,上面函数的逆函数(例如 DOMobject.ele
我一直在尝试在集合中嵌套集合,但无济于事。 这个概念来自 photoshop,您可以在其中将元素分组到一个文件夹/集中,然后将它们嵌套到另一个文件夹/集中。 我正在尝试轻松地将两组或更多组合并为一组,
我正在尝试绘制一个矩形,每条边(不在角落)有 4 个连接点。像这样: 因为稍后我需要对它们使用鼠标事件。 最佳答案 是this你在找什么? 关于raphael - 使用 Raphael 绘制带连接点的
您好,我正在按照此页面上的教程进行操作: http://net.tutsplus.com/tutorials/javascript-ajax/an-introduction-to-the-raphae
var paper = new Raphael("holder", 320, 200); function testPaper(){
使用 Raphael 2.0 ,我试图以相对于集合中所有对象的方式将变换应用于一组对象。但是,我得到的效果是,无论集合中的其他对象如何,变换都单独应用于每个项目。 例如:http://jsfiddle
我想随着鼠标在纸上的移动画一条线。我只知道如何使用 path 画线。但是想知道是否有人有想法与 mousemove 一起画线。请帮我解决这个问题。 最佳答案 这里有一个使用 raphael 的例子的链
我需要使用 raphael.js 绘制各种大小的同心圆弧。我试图理解背后的代码 http://raphaeljs.com/polar-clock.html ,这与我想要的非常相似,但是,没有评论,很难
我想让堆叠条形图的每个部分(在我的例子中是水平的)都可以单击到特定的超链接(基本上是“向下钻取”)。我认为 g.bar.js 中没有这个功能,但是有人这样做过或者可以为我指出正确的方向吗?在点图上也很
我想用按钮点击事件显示/隐藏我的 raphael svg 图 请知道该怎么做的人。请帮助我 我尝试通过这种方式来做,但它不起作用。 var p = Raphael(900,70,200,200); p
我找不到简单答案的简单问题。 如何查看 Raphael 元素的完整当前人类可读(非矩阵)变换状态,无论该元素的变换状态是否设置或如何设置? 例如,使用 element.transform() 不会为您
我一直在使用raphael.draggable与我目前正在 build 的项目。我已经升级到raphael 2.0现在插件似乎停止工作了,我不知道为什么。 我得到的错误信息是: paper.dragg
我需要检索在 Raphael 中绘制的路径末端的 X/Y 坐标。我找到了一种通过在 SVG 浏览器中自省(introspection)路径的方法,但这种方法在 VML 浏览器中不起作用。 例子: va
我正在将 Raphael 与 Meteor 一起使用,但遇到了问题。我正在创建一个 paper通过使用 var paper = Raphael("paper", 800, 600);如果我将此代码放在
我正在研究项目要使用什么图形,Raphael.js 成为了最佳竞争者。但是,在阅读示例代码和文档时,它显示 Raphael 创建了一个 Canvas (通过 homepage 上的 paper 变量)
出于某种原因,我可以使用矩形变量而不是圆形来使用它。 目前,此代码允许两个圆圈独立拖动,但不能一起拖动 有人知道如何解决这个问题或其他方法吗? addIntermediateSymbol = func
我很难在 Raphael.js 中调整 Canvas 的大小。我以为我可以简单地使用: var paper = Raphael("paper", 100, 100); 这将使用 ID“纸”创建一个宽度
我想使用 raphael.js 渲染 ember.js View 或 d3.js .我的理解是it is possible to do this ,但在阅读了所有文档和谷歌搜索后,我似乎无法弄清楚如何
我认为所有这些基本上都非常酷,并且熟悉它们的明显差异(SVG 与 Canvas ),但我不确定它们在开发时间、性能和支持方面的潜在差异、优势或限制。 最佳答案 我体验过 Processing 作为一个
我最近使用 d3.js 实现了一些数据可视化,我现在正试图让它适用于 Internet Explorer 浏览器版本 7 及更高版本。让它工作的常见建议是将 d3.js 与 raphael.js 结合
我是一名优秀的程序员,十分优秀!