phaser/src/gameobjects/image/ImageCanvasRenderer.js

128 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-10-04 21:36:07 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @author Mat Groves (@Doormat23)
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
2016-10-04 21:36:07 +00:00
*/
Phaser.Renderer.Canvas.GameObjects.Image = {
2016-10-04 21:36:07 +00:00
TYPES: [
Phaser.GameObject.Image.prototype
],
render: function (renderer, src)
2016-10-04 21:36:07 +00:00
{
var frame = src.frame;
var source = frame.source;
// Skip render?
if (!src.visible || !src.alpha || !src.renderable || !frame.cutWidth || !frame.cutHeight)
2016-10-04 21:36:07 +00:00
{
return;
}
// Blend Mode
2016-10-04 21:36:07 +00:00
if (src.blendMode !== renderer.currentBlendMode)
2016-10-04 21:36:07 +00:00
{
renderer.currentBlendMode = src.blendMode;
renderer.context.globalCompositeOperation = renderer.blendModes[renderer.currentBlendMode];
2016-10-04 21:36:07 +00:00
}
// Alpha
2016-10-04 21:36:07 +00:00
if (src.worldAlpha !== renderer.context.globalAlpha)
{
renderer.context.globalAlpha = src.worldAlpha;
}
2016-10-04 21:36:07 +00:00
// Smoothing (should this be a Game Object, or Frame/Texture level property?)
if (source.scaleMode !== renderer.currentScaleMode)
2016-10-04 21:36:07 +00:00
{
renderer.currentScaleMode = source.scaleMode;
renderer.context[renderer.smoothProperty] = (source.scaleMode === Phaser.scaleModes.LINEAR);
2016-10-04 21:36:07 +00:00
}
var wt = src.worldTransform;
var resolution = source.resolution / renderer.game.resolution;
var dx = frame.x - (src.anchor.x * frame.width);
var dy = frame.y - (src.anchor.y * frame.height);
2016-10-04 21:36:07 +00:00
var tx = (wt.tx * renderer.game.resolution) + renderer.game.camera._shake.x;
var ty = (wt.ty * renderer.game.resolution) + renderer.game.camera._shake.y;
// Round Pixels
2016-10-04 21:36:07 +00:00
if (renderer.roundPixels)
{
tx |= 0;
ty |= 0;
dx |= 0;
dy |= 0;
}
var cw = frame.cutWidth;
var ch = frame.cutHeight;
renderer.context.setTransform(wt.a, wt.b, wt.c, wt.d, tx, ty);
renderer.context.drawImage(source.image, frame.cutX, frame.cutY, cw, ch, dx, dy, cw / resolution, ch / resolution);
/*
// Move this to either the Renderer, or the Texture Manager, but not here (as it's repeated all over the place)
if (src.texture.rotated)
2016-10-04 21:36:07 +00:00
{
var a = wt.a;
var b = wt.b;
var c = wt.c;
var d = wt.d;
var e = cw;
// Offset before rotating
tx = wt.c * ch + tx;
ty = wt.d * ch + ty;
// Rotate matrix by 90 degrees
// We use precalculated values for sine and cosine of rad(90)
wt.a = a * 6.123233995736766e-17 + -c;
wt.b = b * 6.123233995736766e-17 + -d;
wt.c = a + c * 6.123233995736766e-17;
wt.d = b + d * 6.123233995736766e-17;
// Update cropping dimensions.
cw = ch;
ch = e;
}
*/
2016-10-04 21:36:07 +00:00
/*
if (src.tint !== 0xFFFFFF)
2016-10-04 21:36:07 +00:00
{
if (src.texture.requiresReTint || src.cachedTint !== src.tint)
2016-10-04 21:36:07 +00:00
{
src.tintedTexture = PIXI.CanvasTinter.getTintedTexture(src, src.tint);
2016-10-04 21:36:07 +00:00
src.cachedTint = src.tint;
src.texture.requiresReTint = false;
2016-10-04 21:36:07 +00:00
}
renderer.context.drawImage(src.tintedTexture, 0, 0, cw, ch, dx, dy, cw / resolution, ch / resolution);
2016-10-04 21:36:07 +00:00
}
else
{
var cx = src.texture.crop.x;
var cy = src.texture.crop.y;
2016-10-04 21:36:07 +00:00
renderer.context.drawImage(src.texture.baseTexture.source, cx, cy, cw, ch, dx, dy, cw / resolution, ch / resolution);
2016-10-04 21:36:07 +00:00
}
*/
2016-10-04 21:36:07 +00:00
}
};