微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Vue.js中将动态高度和宽度设置为Konva Stage

如何解决在Vue.js中将动态高度和宽度设置为Konva Stage

我想根据其父容器的宽度设置Konva的高度和宽度。当我在舞台上使用静态值时,

stageConfiguration:{ height: innerWidth * .5,width:innerWidth * 0.5}

每次我调整屏幕大小时都必须刷​​新。有没有一种方法可以将动态高度和宽度设置为 stageConfiguration 。当前,我一直坚持这一观点,并在寻找一种设置动态高度和宽度的方法

<template>
<div class="konvaContainer">
   <v-stage :config="stageConfiguration" >
      <v-layer>
             
      </v-layer>
   </v-stage>
</div>
</template>
<script>
export default {
  components: {
    Loading
  },data: () => ({
    stageConfiguration: {}
  )},methods:
  {
    responsiveStage : function() {
    var container = document.querySelector('.konvaContainer');
    var width = container.clientWidth;
    const stageWidth= width*.5;
    const stageHeight= width *.5;
    this.stageConfiguration ={
      width:stageWidth,height:stageHeight
    }
  }}
}
</script>

解决方法

您可以在窗口中使用ResizeObserverresize事件来检测大小变化。

这是ResizeObserver的解决方案:

<template>
  <div class="container">
    <v-stage ref="stage" :config="stageSize">
      <v-layer>
        <v-circle
          :config="{
            x: stageSize.width / 2,y: stageSize.height / 2,radius: stageSize.width / 2,fill: 'green',}"
        />
      </v-layer>
    </v-stage>
  </div>
</template>

<style>
.container {
  width: 100vw;
  height: 100vh;
}
</style>
<script>
export default {
  data() {
    return {
      stageSize: {
        width: 10,height: 10,},};
  },mounted() {
    const container = document.querySelector(".container");
    const observer = new ResizeObserver(() => {
      this.stageSize.width = container.offsetWidth;
      this.stageSize.height = container.offsetHeight;
    });
    observer.observe(container);
  },};
</script>

演示:https://codesandbox.io/s/vue-konva-change-stage-size-or-resize-tbwec?file=/src/App.vue

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?