如何计算与CSS3创建的边框半径相匹配的曲线上的点?几何天才?
我有一个用 css3 边框半径(图像部分)创建的弯曲 div。我旁边有文本行,我想将其与曲线对齐 20px 左右,如下所示(无法发布图像,不记得旧的登录信息):
技巧是曲线根据窗口大小而变化,所以我希望能够计算曲线上文本应该偏移的点,本质上创建一个真正的手动文本换行。
最终我需要能够使用 javascript 进行计算和更新。
(编辑后按评论添加):用于演示目的的曲线 css 是 左下边框半径:316px 698px; 但这是根据脚本根据页面大小计算的。 另外,值得一提的是,我根本不需要支持 IE 或 FireFox——只需支持 webkit(独立的信息亭应用程序)。
I have a curved div created with css3 border radius (the image part). I have text lines next to it that I would like to align 20px or so off the curve, like so (cant post images, cant remember old login):
The trick is the curve changes depending on the window size, so I'd like to be able to calculate the points on the curve that the text should offset from, essentially creating a really manual text-wrap.
Ultimately I need to be able to calculate and update with javascript.
(edited to add per comment below): The curve css for purposes of demonstration is
border-bottom-left-radius: 316px 698px;
but that is calculated based on page size by a script.
Also, good to mention, is I have no requirement to support IE or FireFox at all--just webkit (standalone kiosk application).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 duri 评论中所建议的,您可以使用圆方程:
http://www.wolframalpha.com/input/?i=%28x -5%29^2%2B%28y%2B4%29^2%3D25(其中中心位于5;-4 r=5)
但是,我使用贝塞尔曲线用 JavaScript 绘图。它们非常灵活,由 2 个向量组成,它们形成的曲线从第一个向量的初始点开始,到第二个向量的初始点结束。
更多:http://en.wikipedia.org/wiki/B%C3%A9zier_curve
(请注意,绘制圆矢量的一部分将是垂直的)
As suggested in comment by duri, you can use circle equation:
http://www.wolframalpha.com/input/?i=%28x-5%29^2%2B%28y%2B4%29^2%3D25 (where center is in 5;-4 r=5)
However, I was using Bezier's Curves for drawing in Javascript. They are very flexible and consist or 2 vectors, the curve made by them starts in first vector's initial point and finish in second vector's.
More: http://en.wikipedia.org/wiki/B%C3%A9zier_curve
(Notice that for drawing part of the circle vector will be perpendicular)
好吧,我已经玩了一段时间了。这是一个早期的概念;它有点低效,不能与滚动条一起使用,并且到目前为止它(或多或少)仅在 Internet Explorer 9 和 Firefox 5 中工作(我没有测试这些浏览器的其他版本)。我跳过了数学方面的内容,并以另一种方式做了 - 我使用 document.elementFromPoint 来找出曲线范围。这就是它在 Chrome 中不起作用的原因 - 它的 elementFromPoint 实现不尊重 border-radius (查看 这个示例);因此我可能不得不重新设计整个脚本。尽管如此,我还是向您展示了我创建的内容,因为它可能会给其他愿意帮助您的人带来很好的启发。我会努力改进我的剧本;当我取得一些进展时我会通知你。
该脚本可以在 http://94.136.148.82/~duri 找到/teds-curve/1.html
Okay, I've played with it for a while. This is an early concept; it is somewhat inefficient, doesn't work with scrollbars and so far it works (more or less) only in Internet Explorer 9 and Firefox 5 (I didn't test other versions of these browsers). I skipped that mathematical stuff and did it another way - I use document.elementFromPoint to find out where the curve ranges. This is why it doesn't work in Chrome - its implementation of elementFromPoint doesn't respect border-radius (look at this example); hence I'll probably have to remodel the whole script. Nevertheless I show you what I created because it could be a good inspiration for another people who are willing to help you. I'll try to improve my script; I'll let you know when I make some progress.
The script can be found at http://94.136.148.82/~duri/teds-curve/1.html
要计算圆上一点的位置,可以使用以下公式:
其中 c = 半径,a 是距中心的垂直距离,b 是距中心的水平距离。
因此,了解了这一点,我构建了一个非常人为的示例供您回顾。请注意,有一些事情可以帮助提高性能,例如缓存半径平方,但我省略了它以避免使演示复杂化。
To work out the position of a point along a circle you can use the formula:
where c = radius, a is verticle distance from center, b is horizontal distance from center.
So knowing this, I constructed a very contrived example for you to review. Please note there are a couple of things to help increase performance such as caching the radius squared but I left it out to avoid complicating the demo.
如果您可以使用 jQuery,我创建了一个名为 jCurvy 的 jQuery 插件,它允许您沿着贝塞尔曲线定位元素。
您需要调整传递到曲线函数中的参数以匹配不断变化的曲线,这可能很棘手。你的曲线变化了多少?
If you can use jQuery I have created a jQuery pluging called jCurvy that will allow you to position elements along a bezier curve.
You would need to adjust the parameters that you are passing into the curve function in order to match the changing curve, which may be tricky. How much is your curve changing?