vue-drawing-canvas
VueJS Component for drawing on canvas.
Support for both Vue 3 and Vue 2 + Composition API
Demo
Vue 3

Vue 2
Deployed on a nuxt container which have access to terminal

Note:
If you're using nuxt.js and receive error Object(...) is not a function
please refer to this issue
Table of Contents
Installation
Install using package manager:
npm install --save-dev vue-drawing-canvas
npm install --save-dev vue-drawing-canvas @vue/composition-api
Then add it to your component files
<template>
<vue-drawing-canvas ref="VueCanvasDrawing" />
</template>
<script>
import VueDrawingCanvas from "vue-drawing-canvas";
export default {
name: "MyComponent",
components: {
VueDrawingCanvas,
},
};
</script>
Usage
Props
Name | Type | Default Value | Description |
---|
canvasId | String | VueDrawingCanvas | Specifies your canvas id |
width | String, Number | 600 | Specifies canvas width |
height | String, Number | 400 | Specifies canvas height |
image | String | | Your v-model to get canvas output to an base64 image |
strokeType | String | "dash" | Specifies stroke type to draw on canvas.
Accepted value "dash" , "line" , "circle" , "square" , "triangle" , "half_triangle" |
fillShape | Boolean | false | Specifies if the shape must be filled with the current fill style |
eraser | Boolean | false | Props to change state from drawing to erasing |
color | String | "#000000" | Specifies the color, gradient, or pattern to use for the strokes |
lineWidth | Number | 5 | Sets the thickness of line |
lineCap | String | "round" | Determines the shape used to draw the end points of line.
Accepted value "round" , "square" , "butt" . Refer to this site for more information. |
lineJoin | String | "miter" | determines the shape used to join two line segments where they meet.
Accepted value "round" , "miter" , "square" . Refer to this site for more information. |
lock | Boolean | false | Lock canvas for drawing |
backgroundColor | String | "#FFFFFF" | Set background color on your canvas |
backgroundImage | String | | Set background image on your canvas
Be carefull for performance issue when using this props !! |
initialImage | Array | [] | Draw strokes and shapes from canvas you've worked before. Demo |
additionalImages | Array | [] | Accept Array of watermark Object to draw either text or insert multiple image on canvas
Be carefull for performance issue when using this props !! |
classes | Array, String, Object | | Specifies your own classes to canvas |
styles | Array, String, Object | | Specifies your own styles to canvas |
watermark | Object | | Put watermark text/image on your image output
(see details in the next section below) |
saveAs | String | "png" | Specifies output type. This props accept either "png" or "jpeg" |
outputWidth | Number | this.width | Specifies image output width, if undefined then canvas width will be used |
outputHeight | Number | this.height | Specifies image output height, if undefined then canvas height will be used |
Watermark Object
{
type: "Text",
source: "Watermark",
x: 200,
y: 180,
imageStyle: {
width: 600,
height: 400
},
fontStyle: {
width: 200,
lineHeight: 48,
color: '#FF0000',
font: 'bold 48px serif',
drawType: 'fill',
textAlign: 'left',
textBaseline: 'top',
rotate: 45
}
}
Methods
Method Name | Return Value | Description |
---|
getCoordinates(event) | { x: 0, y: 0 } | Get x-axis and y-axis coordinates from current canvas |
reset() | | Reset current canvas to new state |
undo() | | Remove last drawing stroke on current canvas |
redo() | | Re-draw last removed stroke on current canvas |
redraw() | | Redraw all strokes on current canvas |
isEmpty() | true or false | Get current canvas empty state |
getAllStrokes() | Array of strokes and shapes | Get all strokes and shapes from canvas |
Changelog
Read here
License
MIT