如何防止 canvas 溢出卡并使其在 vuetify 中响应?

How to prevent canvas from overflowing the card and make it responsive in vuetify?

上下文:

嗨,我正在尝试在 vuetify 中使用 fabricjs canvas 并使其在所有屏幕上看起来都具有响应性。 但目前,我面临 canvas 没有根据卡片重新调整大小,而是溢出卡片的问题。 我试过使用 v-responsive 但它没有将宽高比应用于 canvas,可能是我使用它的方式不正确。

任何建议都会有所帮助。

这就是现在的情况

这就是我想要达到的目标

结构

模拟:

代码

这是我迄今为止尝试过的方法。

<v-layout>
  <v-flex xs12>
    <v-container class="red" fluid>
      <v-layout row wrap align-center justify-center>
        <v-flex xs3 sm3 md3 class="ma-2" class="purple">
          <v-card flat tile class="yellow">
            <v-card-title
              id="fabric-canvas-wrapper"
              class="green pb-0 justify-center"
            >
              <v-responsive aspect-ratio="4/3" class="mx-auto px-3">
                <canvas id="c"> </canvas>
              </v-responsive>
            </v-card-title>

            <v-card-text class="blue text-xs-center py-0">
              <p class=".body-2 pa-2 text-truncate">Kangaroo Valley Safari</p>
            </v-card-text>
          </v-card>
        </v-flex>
      </v-layout>
    </v-container>
  </v-flex>
</v-layout>

https://codepen.io/adatdeltax/pen/OJWOPBo

A canvas element can be sized arbitrarily by a style sheet, its bitmap is then subject to the 'object-fit' CSS property.

Source

canvas 元素的 width/height 与 canvas 元素位图的 width/height 不同。这意味着您只能使用 CSS 样式来解决此问题。

canvas {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Example

如果你想保持宽高比,你可以使用填充技巧。

.canvas-container {
  width: 100%;
  padding-top: 100%; // for 1:1 ratio
}

Example