phaser/v3/src/gameobjects/graphics/Graphics.js

159 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-02-28 14:49:39 +00:00
var Class = require('../../utils/Class');
var GameObject = require('../GameObject');
var Components = require('../../components');
var Render = require('./GraphicsRender');
2017-02-28 21:12:14 +00:00
var Commands = require('./Commands');
var MATH_CONST = require('../../math/const');
2017-02-28 14:49:39 +00:00
var Graphics = new Class({
Mixins: [
Components.Alpha,
Components.BlendMode,
Components.Transform,
Components.Visible,
Render
],
initialize:
function Graphics (state, x, y)
{
GameObject.call(this, state);
2017-02-28 14:49:39 +00:00
this.setPosition(x, y);
this.commandBuffer = [];
2017-02-28 16:52:09 +00:00
},
arc: function (x, y, radius, startAngle, endAngle, anticlockwise)
2017-02-28 21:12:14 +00:00
{
this.commandBuffer.push(
Commands.ARC,
x, y, radius, startAngle, endAngle, anticlockwise
);
2017-02-28 16:52:09 +00:00
},
lineStyle: function (lineWidth, color, alpha)
2017-02-28 21:12:14 +00:00
{
this.commandBuffer.push(
Commands.LINE_STYLE,
lineWidth, color, alpha
2017-02-28 21:12:14 +00:00
);
2017-02-28 16:52:09 +00:00
},
fillStyle: function (color, alpha)
2017-02-28 21:12:14 +00:00
{
if (alpha === undefined) { alpha = 1; }
2017-02-28 21:12:14 +00:00
this.commandBuffer.push(
Commands.FILL_STYLE,
color, alpha
2017-02-28 21:12:14 +00:00
);
2017-02-28 16:52:09 +00:00
},
beginPath: function ()
{
this.commandBuffer.push(
Commands.BEGIN_PATH
);
},
closePath: function ()
{
this.commandBuffer.push(
Commands.CLOSE_PATH
);
},
fillPath: function ()
{
this.commandBuffer.push(
Commands.FILL_PATH
);
},
strokePath: function ()
{
this.commandBuffer.push(
Commands.STROKE_PATH
);
},
fillCircle: function (x, y, radius)
{
this.beginPath();
this.arc(x, y, radius, 0, MATH_CONST.PI2);
this.fillPath();
this.closePath();
},
fillRect: function (x, y, width, height)
{
this.commandBuffer.push(
Commands.FILL_RECT,
x, y, width, height
);
},
fillTriangle: function (x0, y0, x1, y1, x2, y2)
{
this.commandBuffer.push(
Commands.FILL_TRIANGLE,
x0, y0, x1, y1, x2, y2
);
},
strokeCircle: function (x, y, radius)
2017-02-28 21:12:14 +00:00
{
this.beginPath();
this.arc(x, y, radius, 0, MATH_CONST.PI2);
this.closePath();
this.strokePath();
2017-02-28 16:52:09 +00:00
},
2017-02-28 14:49:39 +00:00
strokeRect: function (x, y, width, height)
2017-02-28 21:12:14 +00:00
{
this.beginPath();
this.moveTo(x, y);
this.lineTo(x + width, y);
this.lineTo(x + width, y + height);
this.lineTo(x, y + height);
this.lineTo(x, y);
this.strokePath();
this.closePath();
2017-02-28 21:12:14 +00:00
},
2017-02-28 16:52:09 +00:00
strokeTriangle: function (x0, y0, x1, y1, x2, y2)
{
this.commandBuffer.push(
Commands.STROKE_TRIANGLE,
x0, y0, x1, y1, x2, y2
);
},
lineTo: function (x, y)
2017-02-28 21:12:14 +00:00
{
this.commandBuffer.push(
Commands.LINE_TO,
x, y
);
},
2017-02-28 16:52:09 +00:00
moveTo: function (x, y)
2017-02-28 21:12:14 +00:00
{
this.commandBuffer.push(
Commands.MOVE_TO,
x, y
);
2017-02-28 16:52:09 +00:00
},
clear: function ()
2017-02-28 21:12:14 +00:00
{
this.commandBuffer.length = 0;
2017-02-28 16:52:09 +00:00
}
2017-02-28 14:49:39 +00:00
});
module.exports = Graphics;