您当前的位置: 首页 > 

彭世瑜

暂无认证

  • 2浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

中秋赏月:基于vite.js+vue3.js+fabric.js在线制作月饼

彭世瑜 发布时间:2022-08-23 22:08:12 ,浏览量:2

在这里插入图片描述

MoonCake

在线制作中秋月饼

言简意赅,我主要分享一下思路

体验基于vite.js + vue3.js 的快速开发过程,并将代码发布到github

技术方案

  • vite.js: https://vitejs.dev/
  • vue3.js: https://cn.vuejs.org/index.html
  • fabric.js: http://fabricjs.com/
  • FileSaver.js https://www.npmjs.com/package/file-saver

开始项目

pnpm create vite

选择vue模板

核心代码


  
    
      
    

    
      制作月饼
    
  



// created at 2022-08-23
import { fabric } from 'fabric'
import FileSaver from 'file-saver'

export default {
  name: 'App',

  props: {},

  components: {},

  data() {
    return {
      canvas: null,
    }
  },

  computed: {},

  methods: {
    async getData() {},

    // 导出为图片
    handleExportClick() {
      let base64 = this.canvas.toDataURL('png')
      FileSaver.saveAs(base64, 'mooncake.png')

      // 输出 png 图片可能会打断 canvas 的渲染
      this.canvas.requestRenderAll()
    },
  },

  mounted() {
    const canvas = new fabric.Canvas('canvas', {
      preserveObjectStacking: true, // 被选中时保持原有层级
      // selection: false,
    })
    // canvas.selection = false

    // 绘制一个圆
    const circle = new fabric.Circle({
      // top: 100,
      // left: 100,
      radius: 100, // 圆的半径 50
      fill: 'rgb(250,201,81)',
    })

    canvas.add(circle)

    circle.viewportCenter()

    // 绘制月饼馅
    const text = new fabric.Textbox('五仁月饼', {
      fill: 'rgb(180,110,48)',
    })
    canvas.add(text)

    text.viewportCenter()

    this.canvas = canvas
    // console.log(canvas.getObjects())
  },

  created() {
    this.getData()
  },
}





.app {
  text-align: center;
}

.canvas-warp {
  border: 1px solid #ccc;
  box-sizing: border-box;
}

#canvas {
  width: 100%;
  height: 100%;
}

@media only screen and (max-width: 768px) {
  .app {
    padding: 0 8px;
  }

  .button {
    width: 80%;
    padding: 20px 0;
    font-size: 1.5rem;
  }

  .canvas-warp{
    padding: 10px;
  }
}


整体不难,调用Fabric.js 的接口,绘制一个基本的圆,就是月饼,再绘制一个文字,就完成了月饼的制作。

Fabric.js还提供了编辑功能,可以将文字修改为你想要的月饼馅,然后导出图片,直接将绘制完成的月饼下载到本地,发给你想要送月饼的人即可。

最后还通过github提供的github Actions 自动将提交的代码打包发布到github page。

这样就完成了项目创建到打包发布的完整过程。

源码:https://github.com/mouday/moon-cake

在线体验制作月饼:https://mouday.github.io/moon-cake/

在这里插入图片描述

参考文章 Fabric.js 从入门到________: https://juejin.cn/post/7026941253845516324#heading-15 Canvas如何做款祝福月饼【中秋特别版】 https://juejin.cn/post/7005356616685977630 CSS 按钮 https://www.runoob.com/css3/css3-buttons.html

关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.1097s