javascript - 如何“就地”旋转和倾斜 SVG 矩形?
问题描述
我正在玩 SVG,但我被一些东西难住了。我正在尝试使用skew
and将粉色方块变成钻石rotate
。但是,我遇到了奇怪的行为,我无法弄清楚如何克服。
添加skew
, 得到我想要的菱形效果,但是我需要旋转并重新定位它,使其与圆圈对齐。
<rect x="126" y="0" width="40" height="40"fill="pink" transform="skewY(10)" />
但是,当我对 应用旋转transform="rotate(45)"
时rect
,它不会“就地”旋转,而是相对于页面的角落旋转[我认为]。
<rect x="126" y="0" width="40" height="40"fill="pink" transform="skewY(10)" />
有谁知道我如何可以自由旋转和倾斜这个矩形(在 SVG 中而不是 CSS 或任何东西中)而不会如此疯狂和笨拙地移动?
<h1>Shapes</h1>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="20" cy="20" r="20" fill="blue" stroke="red" ></circle>
<circle cx="62" cy="20" r="20" fill="yellow" stroke="red" ></circle>
<circle cx="104" cy="20" r="20" fill="blue" stroke="red" ></circle>
<rect x="126" y="0" width="40" height="40"fill="pink"/>
<circle cx="188" cy="20" r="20" fill="green" stroke="red" ></circle>
</svg>
解决方案
最简单的就是使用transform-origin和transform-box
rect {
transform-origin: center;
transform-box: fill-box;
}
<h1>Shapes</h1>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="20" cy="20" r="20" fill="blue" stroke="red" ></circle>
<circle cx="62" cy="20" r="20" fill="yellow" stroke="red" ></circle>
<circle cx="104" cy="20" r="20" fill="blue" stroke="red" ></circle>
<rect transform="rotate(45)" x="126" y="0" width="40" height="40"fill="pink"/>
<circle cx="188" cy="20" r="20" fill="green" stroke="red" ></circle>
</svg>
推荐阅读
- python - 如何在用户登录后关闭 login.html 文件,使用烧瓶?
- python - 在一个图中可视化多个变量
- python-3.x - Flask-SQLAlchemy Alembic 迁移
- sql - 按任意开始和结束日期对订单表进行分区
- ruby-on-rails - 将设计与 Rails 一起使用并将 authentication_keys 设置为 [:username] 时出现 401 Auth 错误
- angular - 通过 CORDOVA 在 Angular 上构建 .apk 文件
- python - 使用预签名 URL 上传到 S3 时显示进度条
- android - 我的模拟器没有打开这个“Android Emulator 由于内部错误而关闭”。
- c++ - 异常处理中的捕获不会停止程序
- visual-studio - VS2017 发布不会在第一次错误时停止