图像编辑器,添加两个可绘制形状

huangapple go评论56阅读模式
英文:

Tui image editor, adding two drawable shapes

问题

我只需要在我的自定义 Tui 图像编辑器中显示两种形状,一个矩形和一个圆,没有其他的。

我已经添加了矩形,它完美地工作了,但如果我添加另一个函数来添加一个圆,并先绘制其中一个,然后选择另一个,它仍然会绘制第一个项目。(如果你点击一个圆并绘制,然后点击矩形并绘制,它会绘制一个圆)

我不确定如何让它正常运行。

这是我用于圆形和矩形的两个函数。

  const onAddCircle = () => {
    imageEditorRef.current.startDrawingMode('SHAPE');
    if (imageEditorRef.current.getDrawingMode() !== 'SHAPE') {
      const { height } = imageEditorRef.current.getCanvasSize();
      imageEditorRef.current.startDrawingMode('SHAPE');
      imageEditorRef.current.setDrawingShape('circle', {
        fill: 'transparent',
        stroke: COLOR_STATUS_ORANGE,
        strokeWidth: height / 100,
        isRegular: true,
      });
    }
  };
  const onAddRectangle = () => {
    if (imageEditorRef.current.getDrawingMode() !== 'SHAPE') {
      const { height } = imageEditorRef.current.getCanvasSize();
      imageEditorRef.current.startDrawingMode('SHAPE');
      imageEditorRef.current.clearObjects();
      imageEditorRef.current.setDrawingShape('rect', {
        fill: 'transparent',
        stroke: COLOR_STATUS_ORANGE,
        strokeWidth: height / 100,
        isRegular: true,
      });
    }
  };

它们由按钮触发

  <button
   onClick={onAddCircle}
   color="green"
  />
  <button
   onClick={onAddRectangle}
   color="red"
  />

我尝试在函数的开头添加 imageEditorRef.current.clearDrawingShape();,但它说它不是一个函数,因为它不存在。然后我尝试添加 imageEditorRef.current.clearObjects();,但它会清除该形状绘制的所有内容。

我也查看了他们的文档,但没有关于如何在绘制或选择另一个形状之前实际清除 shape cache 的内容。

有人能帮忙吗?

英文:

I need to only show two shapes in my custom Tui image editor, a rectangle and a circle, nothing more nothing less.

I added the rectangle and it works perfectly but if I add another function to add a circle and draw either or first the and then select the other, it still draws the first item. (if you click on a circle and draw, then click on the rectangle and draw then it draws a circle)

I am not sure how I can get this to function properly.

here is my two functions for my circle and rectangle.


  const onAddCircle = () =&gt; {
    imageEditorRef.current.startDrawingMode(&#39;SHAPE&#39;);
    if (imageEditorRef.current.getDrawingMode() !== &#39;SHAPE&#39;) {
      const { height } = imageEditorRef.current.getCanvasSize();
      imageEditorRef.current.startDrawingMode(&#39;SHAPE&#39;);
      imageEditorRef.current.setDrawingShape(&#39;circle&#39;, {
        fill: &#39;transparent&#39;,
        stroke: COLOR_STATUS_ORANGE,
        strokeWidth: height / 100,
        isRegular: true,
      });
    }
  };
  const onAddRectangle = () =&gt; {
    if (imageEditorRef.current.getDrawingMode() !== &#39;SHAPE&#39;) {
      const { height } = imageEditorRef.current.getCanvasSize();
      imageEditorRef.current.startDrawingMode(&#39;SHAPE&#39;);
      imageEditorRef.current.clearObjects();
      imageEditorRef.current.setDrawingShape(&#39;rect&#39;, {
        fill: &#39;transparent&#39;,
        stroke: COLOR_STATUS_ORANGE,
        strokeWidth: height / 100,
        isRegular: true,
      });
    }
  };

and they are triggered by buttons

  &lt;button
   onClick={onAddCircle}
   color=&quot;green&quot;
  /&gt;
 &lt;button
   onClick={onAddRectangle}
   color=&quot;red&quot;
  /&gt;

I've tried adding imageEditorRef.current.clearDrawingShape(); to the beginning of the functions but it says its not a function because it doesn't exist. then I tried adding imageEditorRef.current.clearObjects(); but that clears everything that it draw by that shape.

I've looked their documentation as well and there is nothing on how to actually clear shape cache before drawing or selecting another shape.

Could someone please help ?

答案1

得分: 0

我修复了它!

我将两个功能合并为一个,并添加了一个名为 shape 的属性,它会检查该属性是否设置为“圆形”或“矩形”,如果是,则使用该逻辑,否则清空绘图板。
英文:

I fixed it!

Ive combined both functions into one added a prop called shape, and it checks if the prop is set to either or and then if it is then it uses that logic else it clears the drawing board.

  const drawShape = (Shape) =&gt; {
    const { height } = imageEditorRef.current.getCanvasSize();
    imageEditorRef.current.startDrawingMode(&#39;SHAPE&#39;);
    if (Shape === &#39;circle&#39;) {
      imageEditorRef.current.setDrawingShape(&#39;circle&#39;, {
        fill: &#39;transparent&#39;,
        stroke: COLOR_STATUS_ORANGE,
        strokeWidth: height / 100,
        isRegular: true,
      });
    } else if (Shape === &#39;rectangle&#39;) {
      imageEditorRef.current.setDrawingShape(&#39;rect&#39;, {
        fill: &#39;transparent&#39;,
        stroke: COLOR_STATUS_ORANGE,
        strokeWidth: height / 100,
        isRegular: true,
      });
    } else {
      imageEditorRef.current.stopDrawingMode();
    }
  };

huangapple
  • 本文由 发表于 2023年2月13日 22:48:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定