在canvas
中模拟css
的background-size
、background-position
、background-repeat
三个属性的部分效果。单位仅支持px
、%
。
import drawBackgroundImageToCanvas from './index.js'
let width = 500
let height = 500
let img = '/1.jpg'
let canvas = document.createElement('canvas')
canvas.width = width
canvas.height = height
drawBackgroundImageToCanvas(ctx, width, height, img, {
backgroundRepeat: 'repeat-y',
backgroundSize: '60%',
backgroundPosition: 'center center'
}, (err) => {
if (err) {
// 失败
} else {
// 成功
}
})