首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vue-konva如何开始绘图

vue-konva如何开始绘图
EN

Stack Overflow用户
提问于 2022-01-31 12:41:09
回答 1查看 155关注 0票数 0

需要开始绘制在舞台元素时,在事件鼠标向下。我在正式文档中有带有js的模板和示例。我如何使用线和开始绘制?

代码语言:javascript
复制
<template>
  <v-stage
    ref="stage"
    :config="stageSize"
    @mousedown="handleMousedown"
   
  >
    <v-layer ref="layer">
    </v-layer>
  </v-stage>
</template>

export default {
  data() {
    return {
      stageSize: {
        width: width,
        height: height,
      },
      text: "",
      isDrawing: false,
    };
  },
  methods: {
    handleMousedown() {
      console.log("test");
      this.isDrawing = true;
        
    }
  },
};
</script> 

公文中的例子

代码语言:javascript
复制
stage.on('mousedown touchstart', function (e) {
        isPaint = true;
        var pos = stage.getPointerPosition();
        lastLine = new Konva.Line({
          stroke: '#df4b26',
          strokeWidth: 5,
          globalCompositeOperation:
            mode === 'brush' ? 'source-over' : 'destination-out',
          // round cap for smoother lines
          lineCap: 'round',
          // add point twice, so we have some drawings even on a simple click
          points: [pos.x, pos.y, pos.x, pos.y],
        });
        layer.add(lastLine);
      });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-31 22:58:16

使用"vue-way":

代码语言:javascript
复制
<template>
  <div>
    <v-stage
      ref="stage"
      :config="configKonva"
      @mousedown="handleMouseDown"
      @mousemove="handleMouseMove"
      @mouseup="handleMouseUp"
    >
      <v-layer ref="layer">
        <v-line
          v-for="line in lines"
          :key="line.id"
          :config="{
            stroke: 'black',
            points: line.points,
          }"
        />
      </v-layer>
    </v-stage>
  </div>
</template>

<script>
const width = window.innerWidth;
const height = window.innerHeight;
export default {
  data() {
    return {
      lines: [],
      isDrawing: false,
      configKonva: {
        width: width,
        height: height,
      },
    };
  },
  methods: {
    handleMouseDown(e) {
      this.isDrawing = true;
      const pos = e.target.getStage().getPointerPosition();
      this.lines = [...this.lines, { points: [pos.x, pos.y] }];
    },
    handleMouseMove(e) {
      // no drawing - skipping
      if (!this.isDrawing) {
        return;
      }
      const stage = e.target.getStage();
      const point = stage.getPointerPosition();
      let lastLine = this.lines[this.lines.length - 1];
      // add point
      lastLine.points = lastLine.points.concat([point.x, point.y]);

      // replace last
      this.lines.splice(this.lines.length - 1, 1, lastLine);
    },

    handleMouseUp() {
      this.isDrawing = false;
    },
  },
};
</script>

<style>
body {
  margin: 0;
  padding: 0;
}
</style>

演示:https://codesandbox.io/s/vue-konva-free-drawing-ux2uy?file=/src/App.vue

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70926116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档