html5 canvas防止线宽缩放
如果我画一个线宽=2的矩形,然后将其缩放到矩形大小的两倍,我会得到一个矩形,其边框大小是初始线宽的两倍。
有没有办法将线宽保持在感知尺寸 2 或原始尺寸。
简而言之,我只想缩放矩形的大小,但在视觉上保持线宽大小为 2。
我尝试在scale(2,2) 命令之前和之后设置线宽,但边框宽度也会增加。
一种选择是将线宽除以比例因子,如果 x 和 y 比例因子相同,则此方法有效。
我没有缩放矩形宽度和高度的选项,我需要使用缩放命令,因为矩形内有其他需要缩放的对象。
If I draw a rectangle of say linewidth=2 and then scale it to double the size of the rectangle, I get a rectangle that has its border double the size of the initial linewidth.
Is there a way to keep the linewidth to the perceived size of 2 or the original size.
In short, I want to just scale the size of the rectangle but keep the linewidth visually of size 2.
I tried setting the linewidth before and after the scale(2,2) command but the border width also increases.
One option is to divide the linewidth by the scale factor and this will work if the x and y scale factors are the same.
I don't have the option to scale the rectangle width and height and I need to use the scale command as I have other objects within the rectangle that need the scaling.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用变换来定义路径,而无需变换来描边它。这样线宽就不会改变。
例子:
You can define path with transformation and stroke it without one. That way line width won't be transformed.
Example:
线宽应事先缩小。
The lineWidth should be scaled down beforehand.
好的,您有几个选择:
您可以自己缩放坐标和尺寸,例如
,您还需要将缩放应用于所有其他对象。
或者,您可以应用缩放但不依赖 lineWidth 来绘制矩形的边框。一种简单的方法是通过填充正确的区域然后删除内部并减去边框来绘制矩形,如下所示:
Ok, you have a couple of options:
You can do your own scaling of coordinates and dimensions, e.g.
And you'll need to apply the scaling to all the other objects too.
Alternatively you could apply the scaling but not rely on lineWidth for drawing the border of the rectangle. A simple method would be to draw the rectangle by filling the correct region and then removing the interior, minus the border, like so: