【问题标题】:KineticJS: .SetFill on a polygon updates the fill property but doesn't fill it with colorKineticJS:多边形上的 .SetFill 更新填充属性,但不填充颜色
【发布时间】:2020-01-03 04:47:20
【问题描述】:

我有一个现有的代码库,它创建一个舞台,然后添加一些图层,并在其中一个图层中绘制 KineticJS 多边形。现在,当用户单击舞台外的元素时,我需要更新多边形的背景。

我已经设法获得了舞台细节,然后是我想要更改的确切多边形,还设法使用.SetFill 方法更改了fill 多边形。当我访问多边形的属性时,我看到填充属性设置正确。但是多边形的背景没有改变。

.SetFill 函数之后我们需要做些什么吗?或者,在创建 KineticJS 形状后更改/更新其填充的正确方法是什么?我没有看到任何更新或重绘方法...

代码在 Clojurescript 中,所以我没有确切的 JS 代码,但这些是我拥有的步骤:

  1. polygon.setFill(color hexcode)
  2. layer.add(polygon)
  3. stage.add(layer)
  4. stage.draw()

【问题讨论】:

  • 尝试在更改背景后触发layer.draw()函数,并通过以下资源,可能是由于KineticJS的缓存问题。参考:github.com/lavrton/kineticjs-tips-and-tools
  • 我可以知道为什么这个问题被否决了吗?是不是漏了什么
  • 只有投反对票的人才能回答这个问题:D,无论如何,您是否可以在 jsfiddle 或类似的东西上创建代码的 sn-p?
  • 不幸的是,我不能,因为它在 Clojurescript 中。如果 Clojurescript 中有类似于 JSFiddle 的东西,我会非常喜欢!
  • 我尝试触发 layer.draw() ,但它没有工作。我现在正在尝试其他选择。谢谢顺便说一句

标签: javascript kineticjs


【解决方案1】:

这是一个使用setAttrs 方法更新多边形背景的 kinetic.js 示例。更改后,使用batchDraw方法更新图层。

let index = 0;
const colors = [
  [0, 'yellow', 1, 'red'],
  [0, 'red', 1, 'yellow']
];
document.querySelector('#container').addEventListener('click', function() {
  if (index++ > 0) index = 0;
  polygon.setAttrs({
    fillLinearGradientColorStops: colors[index]
  });
  // force update of layer
  layer.batchDraw();
});

const stage = new Kinetic.Stage({
  container: 'container',
  width: 615,
  height: 145
});
const layer = new Kinetic.Layer();
const polygon = new Kinetic.RegularPolygon({
  x: stage.width() / 2,
  y: stage.height() / 2,
  sides: 5,
  radius: 70,
  fillLinearGradientStartPoint: {
    x: -50,
    y: -50
  },
  fillLinearGradientEndPoint: {
    x: 50,
    y: 50
  },
  fillLinearGradientColorStops: colors[index],
  stroke: 'black',
  strokeWidth: 4,
  draggable: true
});
layer.add(polygon);
stage.add(layer);
stage.draw();
<script src="https://cdnjs.cloudflare.com/ajax/libs/kineticjs/5.2.0/kinetic.min.js"></script>
<div id='container'></div>
<div>Click or drag polygon</div>

【讨论】:

  • 感谢您的努力。 batchDraw 方法也没有更新。我最终做了一个解决方法:删除所有孩子并用填充的颜色重新绘制所有内容。它很丑,但它有效。
猜你喜欢
  • 2013-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
  • 2013-01-10
  • 2020-07-26
相关资源
最近更新 更多