SoFunction
Updated on 2025-04-04

vue uses Canvas to add pictures on canvas

Add images on canvas using Canvas

First create a dom in template

<div style="display:none">
    <img ref="conf0" src="../../assets/image/">
</div>

Then draw the code and write it

// Get Canvas drawinglet myCanvas = this.$
var ctx = ('2d')
 
// Add pictures in Canvas Canvasvar img = this.$refs.conf0
 = () =&gt; {
  (img, 0, 0)
}

Use canvas to add text to the image

&lt;template&gt;
  &lt;div&gt;
    &lt;img src=""
         alt=""
         
         style="width:278px;height:298px"&gt;
    &lt;canvas 
            width="278"
            height="298"&gt;
    &lt;/canvas&gt;
  &lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
export default {
  data () {
    return {

    }
  },
  mounted () {
    ('123#')
  },
  methods: {
    newImage (text) {
      // Generate pictures      var imageBox = ("newimage")
      var canvas = ("canvas")
      var cxt = ("2d")
      var img = new Image()
       = require('@/assets/')
      // The image is loaded before processing can be done       = () =&gt; {
        // Drawing (the canvas and pictures are the same width and height here)        (img, 0, 0)
        // Set the font size         = "36px Microsoft YaHei"
        // After changing the font size, you must reset the alignment, otherwise it will be troublesome to center.  Set the vertical alignment of text         = 'middle'
         = 'center'
        // The position to the left        var left =  / 2
        // The position from the top (picture height - the distance from the bottom of the picture)        var top =  * 0.14
        // Text color         = "#ffffff"
        // The position of the text in the canvas        (text, left, top)
         = ("image/jpg")
        //  = 1
      }
    },
  }
}
&lt;/script&gt;

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.