- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在过去的两天里,我有效地弄清楚了如何不旋转 Raphael Elements。
基本上我正在尝试在元素上实现多个枢轴点以通过鼠标旋转它。
当用户进入旋转模式时,将创建 5 个枢轴。一个用于边界框的每个 Angular ,一个在框的中心。
当鼠标按下并移动时,使用 Raphael 围绕枢轴旋转非常简单 elements.rotate(degrees, x, y)并根据鼠标位置和 atan2 到轴心点计算度数。
问题出现在我旋转了元素、bbox 和其他枢轴之后。相同的 x,y 位置只有视口(viewport)不同。
在支持 SVG 的浏览器中,我可以基于 matrixTransformation and getCTM 创建新的轴心点.然而,在创建第一组新枢轴之后,由于舍入误差,枢轴之后的每次旋转都会远离转换后的 bbox。
以上甚至不是 IE 中的选项,因为它是基于 VML 的,无法解释转换。
Is the only effective way to implement element rotation is by using rotate absolute or rotating around the center of the bounding box?
Is it possible at all the create multi pivot points for an object and update them after mouseup to remain in the corners and center of the transformed bbox?
更新:我尝试使用 jQuery offset 在旋转后找到枢轴,并将该偏移位置用作枢轴点。
演示站点... http://weather.speedfetishperformance.com/dev/raphael/rotation.html
最佳答案
我能想到的最好的跨浏览器方式来做你想做的事情是自己实现旋转而不是让 SVG 来做。旋转 x,y 坐标非常简单,每当我需要进行 2D 旋转时,我都会使用这个 (tcl) 代码:Canvas Rotation .
这样做的好处是您可以最大程度地控制旋转,因为您是手动进行的。这解决了您在旋转后试图猜测最终坐标的问题。此外,这应该是跨浏览器兼容的。
缺点是您必须使用路径。所以没有矩形(尽管将它们转换为路径应该很容易)或省略号(转换为路径有点困难但可行)。此外,由于您是手动执行此操作,因此它应该比让 SVG 为您执行它慢一些。
这是在 javascript 中部分实现的 Tcl 代码:
首先我们需要一个正则表达式来标记 SVG 路径:
var svg_path_regexp = (function(){
var number = '-?[0-9.]+';
var comma = '\s*[, \t]\s*';
var space = '\s+';
var xy = number + comma + number;
var standard_paths = '[mlcsqt]';
var horiz_vert = '[hv]\s*' + number;
var arc = 'a\s*' + xy + space + number + space + xy + space + xy;
var OR = '\s*|';
return new RegExp(
standard_paths +OR+
xy +OR+
horiz_vert +OR+
arc,
'ig'
);
})();
现在我们可以实现旋转功能了:
function rotate_SVG_path (path, Ox, Oy, angle) {
angle = angle * Math.atan(1) * 4 / 180.0; // degrees to radians
var tokens = path.match(svg_path_regexp);
for (var i=0; i<tokens.length; i++) {
var token = tokens[i].replace(/^\s+|\s+$/g,''); // trim string
if (token.match(/\d/)) { // assume it's a coordinate
var xy = token.split(/[, \t]+/);
var x = parseFloat(xy[0]);
var y = parseFloat(xy[1]);
x = x - Ox; // Shift to origin
y = y - Oy;
var xx = x * Math.cos(angle) - y * Math.sin(angle); // Rotate
var yy = x * Math.sin(angle) + y * Math.cos(angle);
x = xx + Ox; // Shift back
y = yy + Oy;
token = x + ',' + y;
}
else if (token.match(/^[hv]/)) {
// handle horizontal/vertical line here
}
else if (token.match(/^a/)) {
// handle arcs here
}
tokens[i] = token;
}
return tokens.join('');
}
上面的旋转函数实现了除水平/垂直线(您需要跟踪之前的 xy 值)和弧线之外的所有内容。两者都不应该太难实现。
关于javascript - Raphael SVG VML 实现旋转的多枢轴点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4686651/
使用 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 结合
我是一名优秀的程序员,十分优秀!