相关文章推荐
帅气的闹钟  ·  夏侯霸为夏侯惇的儿子,作为曹操大将,为何投奔 ...·  3 月前    · 
沉着的毛衣  ·  日本新安保法前世今生:揭开“战争法案”真面目 ...·  3 月前    · 
开朗的烤土司  ·  袁贵仁在高等学校创新能力提升计划工作部署视频 ...·  5 月前    · 
还单身的充电器  ·  web测试点集合- piecesof - 博客园·  7 月前    · 
爱旅游的佛珠  ·  石家河镇_百度百科·  11 月前    · 
小百科  ›  使用KonvaJS绘制和旋转图像开发者社区
const
踏实的长颈鹿
1 年前
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
提问
问 使用KonvaJS绘制和旋转图像
Stack Overflow用户
提问于 2020-03-20 22:50:00
EN

所以我一直在尝试使用Konva来绘制一些图像,文本和形状,一切都很好,直到我尝试在混合中添加旋转。我知道Konva使用原点(默认情况下是左上角,可以通过偏移量移动)来旋转图像,但我需要位置始终是左上角。

因此,我尝试计算、旋转和重新定位对象,以模仿我想要的,但这总是导致图像位置与我的初始状态不同,这是一种不需要的行为。

所以我的问题是,Konva可以从中心旋转图像,同时在旋转后保持左上角的原点位置吗?

*使用代码编辑示例:绿色框/文本被旋转,但位置完全错误。我需要从左到右的位置。

​

const stage = new Konva.Stage({
  container: 'container',
  width: window.innerWidth,
  height: window.innerHeight
const layer = new Konva.Layer();
stage.add(layer);
const rotatePoint= (x, y , rad)=> {
  const rcos = Math.cos(rad);
  const rsin = Math.sin(rad);
  return {
    x: x * rcos - y * rsin,
    y: y * rcos + x * rsin
const rotateAroundCenter = (node, rotation) => {  
  const topLeft = { x: -node.width() / 2, y: -node.height() / 2 };
  const current = rotatePoint(topLeft.x, topLeft.y, Konva.getAngle(node.rotation()));
  const rotated = rotatePoint(topLeft.x, topLeft.y, Konva.getAngle(rotation));
  const dx = rotated.x - current.x,
        dy = rotated.y - current.y;
  node.rotation(rotation); 
  node.x(node.x() + dx);
  node.y(node.y() + dy);
  return node;
let x = 100,
    y = 50, 
    width = 50,
    height = 50;
const rotation = 90;
const text = { width, height, fontSize: 15, fontFamily: 'Calibri', text: 'Simple Text', opacity: 0.5 };
const rect = { width, height, strokeWidth: 2}
let pos = {x, y};
layer.add(new Konva.Circle({ ...pos, radius:5, fill: 'black' })); 
layer.add(new Konva.Text({ ...pos, ...text, fill: 'black' })); 
layer.add(new Konva.Rect({ ...pos, ...rect, stroke: 'purple' })); 
layer.add(new Konva.Text({ ...pos, ...text, fill: 'purple', rotation: rotation })); 
layer.add(new Konva.Rect({ ...pos, ...rect, stroke: 'purple', rotation: rotation }));
pos = {x, y:150};
layer.add(new Konva.Circle({ ...pos, radius:5, fill: 'black' })); 
layer.add(rotateAroundCenter(new Konva.Text({ ...pos, ...text, fill: 'green' }), rotation)); 
layer.add(rotateAroundCenter(new Konva.Rect({ ...pos, ...rect, stroke: 'green' }), rotation)); 
layer.draw();
<!DOCTYPE html>
<meta name="description" content="KonvaJS Template">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/4.2.0/konva.min.js"></script>
  <meta charset="utf-8">
</head>
  <div id="container"></div>
</body>
</html>

​

1 488 0 票数 0
EN
javascript
html5-canvas
konvajs

回答 1

Stack Overflow用户

发布于 2020-03-26 20:51:55

好的,基本上必须按照拉夫顿说的做,把形状放在一个组中(用正确的位置),然后在组内旋转和偏移形状。

​

const stage = new Konva.Stage({
  container: 'container',
  width: 300,
  height: 700
const layer = new Konva.Layer();
stage.add(layer);
const rotatePoint = ({ x, y }, rad) => {
  const rcos = Math.cos(rad);
  const rsin = Math.sin(rad);
  return { x: x * rcos - y * rsin, y: y * rcos + x * rsin };
// will work for shapes with top-left origin, like rectangle
const rotateAroundCenter = (node, rotation) => {
  //current rotation origin (0, 0) relative to desired origin - center (node.width()/2, node.height()/2)
  const topLeft = { x: -node.width() / 2, y: -node.height() / 2 };
  const current = rotatePoint(topLeft, Konva.getAngle(node.rotation()));
  const rotated = rotatePoint(topLeft, Konva.getAngle(rotation));
  const dx = rotated.x - current.x,
        dy = rotated.y - current.y;
  const r = Math.floor(rotation / 90) % 4;  
  switch(r){
    case 1:
    case 3:
      node.x(node.x() + dx + node.height()/2);
      node.y(node.y() + dy + node.width()/2);
      break;
    case 2:
    default: 
      node.x(node.x() + dx + node.width()/2);
      node.y(node.y() + dy + node.height()/2);
      break;
  node.rotation(rotation);
  return node;
let x = 200, y = 250, width = 30, height = 75, text = "? ? ? ? ?", opacity = 0.4;
drawShapes = (x, y, width, height, rotation) => {  
  layer.add(new Konva.Text({x:0, y, fill: 'black', text: (rotation+"°").padStart(5,"_")}));
  layer.add(new Konva.Rect({x, y, width, height, fill: 'black', rotation}));
  layer.add(new Konva.Text({x, y, width, height, fill: 'white', rotation, text}));
  const group = new Konva.Group({ x, y,/* clip:{x:0,y:0,width:200,height:200}*/})
  group.add(rotateAroundCenter(new Konva.Rect({x:-width/2,y:-height/2, width, height,fill: 'grey', opacity}), rotation));
  group.add(rotateAroundCenter(new Konva.Text({x:-width/2,y:-height/2, width, height,fill: 'black', text}), rotation));
  layer.add(group, new Konva.Circle({x, y, radius: 2, fill: 'black'}));
drawShapes(x, 50, width, height,   0);
drawShapes(x, 200, width, height,  90);
drawShapes(x, 350, width, height, 180);
drawShapes(x, 500, width, height, 270);
layer.draw();
<!DOCTYPE html>
<meta name="description" content="KonvaJS Template">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/4.2.0/konva.min.js"></script>
 
推荐文章
帅气的闹钟  ·  夏侯霸为夏侯惇的儿子,作为曹操大将,为何投奔刘备的蜀汉阵营?_ ...
3 月前
沉着的毛衣  ·  日本新安保法前世今生:揭开“战争法案”真面目-中国新闻网
3 月前
开朗的烤土司  ·  袁贵仁在高等学校创新能力提升计划工作部署视频会上的讲话-长沙 ...
5 月前
还单身的充电器  ·  web测试点集合- piecesof - 博客园
7 月前
爱旅游的佛珠  ·  石家河镇_百度百科
11 月前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
小百科 - 百科知识指南
© 2024 ~ 沪ICP备11025650号