2017-02-28 21:12:14 +00:00
|
|
|
var Commands = require('./Commands');
|
2017-03-02 02:06:37 +00:00
|
|
|
var MATH_CONST = require('../../math/const');
|
|
|
|
|
2017-05-11 00:24:57 +00:00
|
|
|
var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, camera, renderTargetCtx)
|
2017-02-28 14:49:39 +00:00
|
|
|
{
|
|
|
|
if (this.renderMask !== this.renderFlags)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-06-22 02:19:03 +00:00
|
|
|
var cameraScrollX = camera.scrollX * src.scrollFactorX;
|
|
|
|
var cameraScrollY = camera.scrollY * src.scrollFactorY;
|
2017-03-02 02:06:37 +00:00
|
|
|
var srcX = src.x;
|
2017-02-28 21:12:14 +00:00
|
|
|
var srcY = src.y;
|
|
|
|
var srcScaleX = src.scaleX;
|
|
|
|
var srcScaleY = src.scaleY;
|
|
|
|
var srcRotation = src.rotation;
|
|
|
|
var commandBuffer = src.commandBuffer;
|
2017-05-11 00:24:57 +00:00
|
|
|
var ctx = renderTargetCtx || renderer.currentContext;
|
2017-02-28 21:12:14 +00:00
|
|
|
var value;
|
2017-03-01 17:30:04 +00:00
|
|
|
var lineAlpha = 1.0;
|
|
|
|
var fillAlpha = 1.0;
|
|
|
|
var lineColor = 0;
|
|
|
|
var fillColor = 0;
|
|
|
|
var lineWidth = 1.0;
|
|
|
|
var red = 0;
|
|
|
|
var green = 0;
|
|
|
|
var blue = 0;
|
2017-02-28 21:12:14 +00:00
|
|
|
|
|
|
|
// Blend Mode
|
|
|
|
if (renderer.currentBlendMode !== src.blendMode)
|
|
|
|
{
|
|
|
|
renderer.currentBlendMode = src.blendMode;
|
|
|
|
ctx.globalCompositeOperation = renderer.blendModes[src.blendMode];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alpha
|
|
|
|
if (renderer.currentAlpha !== src.alpha)
|
|
|
|
{
|
|
|
|
renderer.currentAlpha = src.alpha;
|
|
|
|
ctx.globalAlpha = src.alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Smoothing
|
|
|
|
if (renderer.currentScaleMode !== src.scaleMode)
|
|
|
|
{
|
|
|
|
renderer.currentScaleMode = src.scaleMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.save();
|
2017-03-01 17:56:04 +00:00
|
|
|
ctx.translate(srcX - cameraScrollX, srcY - cameraScrollY);
|
2017-02-28 21:12:14 +00:00
|
|
|
ctx.rotate(srcRotation);
|
|
|
|
ctx.scale(srcScaleX, srcScaleY);
|
|
|
|
ctx.fillStyle = '#fff';
|
|
|
|
ctx.globalAlpha = src.alpha;
|
|
|
|
|
|
|
|
for (var index = 0, length = commandBuffer.length; index < length; ++index)
|
|
|
|
{
|
|
|
|
var commandID = commandBuffer[index];
|
|
|
|
|
2017-03-02 02:06:37 +00:00
|
|
|
switch (commandID)
|
2017-02-28 21:12:14 +00:00
|
|
|
{
|
|
|
|
case Commands.ARC:
|
|
|
|
ctx.arc(
|
2017-03-02 02:06:37 +00:00
|
|
|
commandBuffer[index + 1],
|
|
|
|
commandBuffer[index + 2],
|
|
|
|
commandBuffer[index + 3],
|
|
|
|
commandBuffer[index + 4],
|
|
|
|
commandBuffer[index + 5],
|
2017-02-28 21:12:14 +00:00
|
|
|
commandBuffer[index + 6]
|
|
|
|
);
|
|
|
|
index += 6;
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 17:30:04 +00:00
|
|
|
case Commands.LINE_STYLE:
|
|
|
|
lineWidth = commandBuffer[index + 1];
|
2017-03-02 02:06:37 +00:00
|
|
|
lineColor = commandBuffer[index + 2];
|
2017-03-01 17:30:04 +00:00
|
|
|
lineAlpha = commandBuffer[index + 3];
|
2017-03-08 00:51:09 +00:00
|
|
|
red = ((lineColor & 0xFF0000) >>> 16);
|
|
|
|
green = ((lineColor & 0xFF00) >>> 8);
|
|
|
|
blue = (lineColor & 0xFF);
|
2017-03-08 22:08:20 +00:00
|
|
|
ctx.strokeStyle = 'rgba(' + red + ',' + green + ',' + blue + ',' + lineAlpha + ')';
|
2017-03-08 00:51:09 +00:00
|
|
|
ctx.lineWidth = lineWidth;
|
2017-03-01 17:30:04 +00:00
|
|
|
index += 3;
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 17:30:04 +00:00
|
|
|
case Commands.FILL_STYLE:
|
|
|
|
fillColor = commandBuffer[index + 1];
|
|
|
|
fillAlpha = commandBuffer[index + 2];
|
2017-03-08 00:51:09 +00:00
|
|
|
red = ((fillColor & 0xFF0000) >>> 16);
|
|
|
|
green = ((fillColor & 0xFF00) >>> 8);
|
|
|
|
blue = (fillColor & 0xFF);
|
2017-03-08 22:08:20 +00:00
|
|
|
ctx.fillStyle = 'rgba(' + red + ',' + green + ',' + blue + ',' + fillAlpha + ')';
|
2017-03-01 17:30:04 +00:00
|
|
|
index += 2;
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 17:30:04 +00:00
|
|
|
case Commands.BEGIN_PATH:
|
2017-02-28 21:12:14 +00:00
|
|
|
ctx.beginPath();
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 17:30:04 +00:00
|
|
|
case Commands.CLOSE_PATH:
|
2017-02-28 21:12:14 +00:00
|
|
|
ctx.closePath();
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 17:30:04 +00:00
|
|
|
case Commands.FILL_PATH:
|
|
|
|
ctx.fill();
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 17:30:04 +00:00
|
|
|
case Commands.STROKE_PATH:
|
|
|
|
ctx.stroke();
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 17:30:04 +00:00
|
|
|
case Commands.FILL_RECT:
|
2017-02-28 21:12:14 +00:00
|
|
|
ctx.fillRect(
|
|
|
|
commandBuffer[index + 1],
|
|
|
|
commandBuffer[index + 2],
|
|
|
|
commandBuffer[index + 3],
|
|
|
|
commandBuffer[index + 4]
|
|
|
|
);
|
|
|
|
index += 4;
|
|
|
|
break;
|
2017-03-08 00:51:09 +00:00
|
|
|
|
|
|
|
case Commands.FILL_TRIANGLE:
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(commandBuffer[index + 1], commandBuffer[index + 2]);
|
|
|
|
ctx.lineTo(commandBuffer[index + 3], commandBuffer[index + 4]);
|
|
|
|
ctx.lineTo(commandBuffer[index + 5], commandBuffer[index + 6]);
|
|
|
|
ctx.lineTo(commandBuffer[index + 1], commandBuffer[index + 2]);
|
|
|
|
ctx.closePath();
|
|
|
|
ctx.fill();
|
|
|
|
index += 6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Commands.STROKE_TRIANGLE:
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(commandBuffer[index + 1], commandBuffer[index + 2]);
|
|
|
|
ctx.lineTo(commandBuffer[index + 3], commandBuffer[index + 4]);
|
|
|
|
ctx.lineTo(commandBuffer[index + 5], commandBuffer[index + 6]);
|
|
|
|
ctx.lineTo(commandBuffer[index + 1], commandBuffer[index + 2]);
|
|
|
|
ctx.closePath();
|
|
|
|
ctx.stroke();
|
|
|
|
index += 6;
|
|
|
|
break;
|
|
|
|
|
2017-02-28 21:12:14 +00:00
|
|
|
case Commands.LINE_TO:
|
|
|
|
ctx.lineTo(
|
|
|
|
commandBuffer[index + 1],
|
|
|
|
commandBuffer[index + 2]
|
|
|
|
);
|
|
|
|
index += 2;
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-02-28 21:12:14 +00:00
|
|
|
case Commands.MOVE_TO:
|
|
|
|
ctx.moveTo(
|
|
|
|
commandBuffer[index + 1],
|
|
|
|
commandBuffer[index + 2]
|
|
|
|
);
|
|
|
|
index += 2;
|
|
|
|
break;
|
2017-03-01 17:30:04 +00:00
|
|
|
|
2017-03-15 19:23:10 +00:00
|
|
|
case Commands.LINE_FX_TO:
|
2017-03-14 22:13:31 +00:00
|
|
|
ctx.lineTo(
|
|
|
|
commandBuffer[index + 1],
|
|
|
|
commandBuffer[index + 2]
|
|
|
|
);
|
2017-03-15 19:23:10 +00:00
|
|
|
index += 5;
|
2017-03-14 22:13:31 +00:00
|
|
|
break;
|
|
|
|
|
2017-03-15 19:23:10 +00:00
|
|
|
case Commands.MOVE_FX_TO:
|
2017-03-14 22:13:31 +00:00
|
|
|
ctx.moveTo(
|
|
|
|
commandBuffer[index + 1],
|
|
|
|
commandBuffer[index + 2]
|
|
|
|
);
|
2017-03-15 19:23:10 +00:00
|
|
|
index += 5;
|
2017-03-14 22:13:31 +00:00
|
|
|
break;
|
|
|
|
|
2017-05-08 20:13:48 +00:00
|
|
|
case Commands.SAVE:
|
|
|
|
ctx.save();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Commands.RESTORE:
|
|
|
|
ctx.restore();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Commands.TRANSLATE:
|
|
|
|
ctx.translate(
|
|
|
|
commandBuffer[index + 1],
|
|
|
|
commandBuffer[index + 2]
|
|
|
|
);
|
|
|
|
index += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Commands.SCALE:
|
|
|
|
ctx.scale(
|
|
|
|
commandBuffer[index + 1],
|
|
|
|
commandBuffer[index + 2]
|
|
|
|
);
|
|
|
|
index += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Commands.ROTATE:
|
|
|
|
ctx.rotate(
|
|
|
|
commandBuffer[index + 1]
|
|
|
|
);
|
|
|
|
index += 1;
|
|
|
|
break;
|
|
|
|
|
2017-02-28 21:12:14 +00:00
|
|
|
default:
|
|
|
|
console.error('Phaser: Invalid Graphics Command ID ' + commandID);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.restore();
|
2017-03-02 02:06:37 +00:00
|
|
|
};
|
2017-02-28 14:49:39 +00:00
|
|
|
|
|
|
|
module.exports = GraphicsCanvasRenderer;
|