处理谷歌地图marker旋转
/* * 功能:使用canvas旋转图标,并生成路径 * 参数:options(Object) * 用途:处理谷歌地图marker旋转 * */ const setRotation = (options) => { return new Promise((resolve, reject) => { let w = options.width || 40 let h = options.height || 40 let canvas = document.createElement('canvas') canvas.width = w canvas.height = h let ctx = canvas.getContext('2d') let angle = options.deg ? options.deg * Math.PI / 180 : 0 let centerX = w / 2 let centerY = h / 2 let dataURL = '' let img = new Image() img.src = options.url img.onload = function() { ctx.clearRect(0, 0, w, h) ctx.save() ctx.translate(centerX, centerY) ctx.rotate(angle) ctx.translate(-centerX, -centerY) ctx.drawImage(img, centerX/2, 0) ctx.restore() dataURL = canvas.toDataURL('image/png') resolve({ size: options.size, origin: options.origin, url: dataURL }) } }) }