function abasolute2CanvasRenderContextText(path, funcName, noFunction) {
let commands = convertstandardCommand(path)
let len = commands.length
let i = 0
let prevCmd
let contextText = noFunction ? '' : `var ${funcName ? funcName : 'draw'} = function(ctx) {\n`
while (i < len) {
let curCmd = commands[i]
switch(curCmd.cmd) {
case 'H':
prevCmd = commands[i - 1]
let _y = prevCmd ? prevCmd.coords.slice(-2, -1) : 0
contextText += ` ctx.lineTo(${curCmd.coords[0]}, ${_y});\n`
break;
case 'V':
prevCmd = commands[i - 1]
let _x = prevCmd ? prevCmd.coords.slice(-1) : 0
contextText += ` ctx.lineTo(${_x}, ${curCmd.coords[0]});\n`
break;
case 'M':
contextText += ` ctx.moveTo(${curCmd.coords.join(', ')});\n`
break;
case 'L':
contextText += ` ctx.lineTo(${curCmd.coords.join(', ')});\n`
break;
case 'Q':
contextText += ` ctx.quadraticCurveTo(${curCmd.coords.join(', ')});\n`
break;
case 'T':
prevCmd = commands[i - 1]
if (prevCmd && (prevCmd.cmd === 'T' || prevCmd.cmd === 'Q')) {
let xy = prevCmd.coords.slice(-2)
let dxdy = prevCmd.coords.slice(-4,-2)
let _dx1 = xy[0] * 2 - dxdy[0]
let _dy1 = xy[1] * 2 - dxdy[1]
curCmd.cmd = 'Q'
curCmd.coords.unshift(_dx1, _dy1)
contextText += ` ctx.quadraticCurveTo(${curCmd.coords.join(', ')});\n`
} else {
throw new Error('ShortHead quadraticCurve is Error')
}
break;
case 'C':
contextText += ` ctx.bezierCurveTo(${curCmd.coords.join(', ')});\n`
break;
case 'S':
prevCmd = commands[i - 1]
if (prevCmd && (prevCmd.cmd === 'S' || prevCmd.cmd === 'C')) {
let xy = prevCmd.coords.slice(-2)
let dxdy = prevCmd.coords.slice(-4,-2)
let _dx1 = xy[0] * 2 - dxdy[0]
let _dy1 = xy[1] * 2 - dxdy[1]
curCmd.cmd = 'C'
curCmd.coords.unshift(_dx1, _dy1)
contextText += ` ctx.bezierCurveTo(${curCmd.coords.join(', ')});\n`
} else {
throw new Error('ShortHead bezierCurve is Error')
}
break;
case 'A':
prevCmd = commands[i - 1]
// console.log('curCmd', curCmd)
let xy = prevCmd ? prevCmd.coords.slice(-2) : [0, 0]
let rx = +curCmd.coords[0]
let ry = +curCmd.coords[1]
let xAxisRotationDeg = +curCmd.coords[2]
let largeArcFlag = +curCmd.coords[3]
let sweepFlag = +curCmd.coords[4]
let x1 = +curCmd.coords[5]
let y1 = +curCmd.coords[6]
console.log(xy[0], xy[1], x1, y1, rx, ry, xAxisRotationDeg, largeArcFlag, sweepFlag)
let ellipseParams = conversionEndPointToCenterPointParameterization(
+xy[0], +xy[1], x1, y1, largeArcFlag, sweepFlag, rx, ry, xAxisRotationDeg)
console.log(ellipseParams)
contextText += ` ctx.ellipse(${ellipseParams.join(', ')});\n`
break;
}
i++;
}
contextText += noFunction ? '' : '}\n'
return contextText
}