0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

nuxtjsにfabricjsを導入するときの手順

Posted at

nuxtjsにfabricjsを導入するときの情報が少なかったのでそのメモです。

必要な項目のインストール

yarn add fabric
yarn add canvas

package.jsonとyarn.lockに追加される。

plugin直下にfabric.jsファイルを作成。

fabric.js
import { fabric } from 'fabric'
import Vue from 'vue'

Vue.use(fabric)
export default fabric

nuxt.config.jsに情報を追加

nuxt.config.js
  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [
    {
      src: '@/plugins/fabric',
      mode: 'client'
    }
  ],

実際のコード

これは赤い四角形を表示するためのサンプル

fabric/index.vue
<template>
  <canvas ref="can" width="200" height="200"></canvas>
</template>

<script>
import { fabric } from 'fabric';

export default {
  mounted() {
    const ref = this.$refs.can;
    const canvas = new fabric.Canvas(ref);
    const rect = new fabric.Rect({
      fill: 'red',
      width: 20,
      height: 20
    });
    canvas.add(rect);
  }
};
</script>

これで localhost:3000/fabricにアクセスすると以下のように赤い四角形が表示されているはず。

Screen Shot 2021-08-15 at 1.28.15.png

参考

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?