Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the rotation of the image in canvas mode. #4325

Merged
merged 1 commit into from May 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 0 additions & 9 deletions cocos2d/core/assets/CCSpriteFrame.js
Expand Up @@ -327,15 +327,6 @@ let SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
}
let w = texture.width, h = texture.height;

if (self._rotated && cc.game.renderType === cc.game.RENDER_TYPE_CANVAS) {
// TODO: rotate texture for canvas
// self._texture = _ccsg.Sprite.CanvasRenderCmd._createRotatedTexture(texture, self.getRect());
self._rotated = false;
w = self._texture.width;
h = self._texture.height;
self._rect = cc.rect(0, 0, w, h);
}

if (self._rect) {
self._checkRect(self._texture);
}
Expand Down
37 changes: 24 additions & 13 deletions cocos2d/core/renderer/canvas/renderers/sprite/simple.js
Expand Up @@ -58,13 +58,13 @@ let renderer = {

if (frame._rotated) {
let l = rect.x;
let r = rect.height;
let r = rect.width;
let b = rect.y;
let t = rect.width;
let t = rect.height;
data[0].u = l;
data[0].v = t;
data[1].u = r;
data[1].v = b;
data[0].v = b;
data[1].u = t;
data[1].v = r;
}
else {
let l = rect.x;
Expand All @@ -83,6 +83,7 @@ let renderer = {
updateVerts (sprite) {
let renderData = sprite._renderData,
node = sprite.node,
frame = sprite.spriteFrame,
data = renderData._data,
cw = node.width, ch = node.height,
appx = node.anchorX * cw, appy = node.anchorY * ch,
Expand All @@ -94,8 +95,7 @@ let renderer = {
t = ch;
}
else {
let frame = sprite.spriteFrame,
ow = frame._originalSize.width, oh = frame._originalSize.height,
let ow = frame._originalSize.width, oh = frame._originalSize.height,
rw = frame._rect.width, rh = frame._rect.height,
offset = frame._offset,
scaleX = cw / ow, scaleY = ch / oh;
Expand All @@ -109,29 +109,40 @@ let renderer = {
t = ch;
}

data[0].x = l;
data[0].y = b;
data[1].x = r;
data[1].y = t;

if (frame._rotated) {
data[0].y = l;
data[0].x = b;
data[1].y = r;
data[1].x = t;
} else {
data[0].x = l;
data[0].y = b;
data[1].x = r;
data[1].y = t;
}

renderData.vertDirty = false;
},

draw (ctx, comp) {
let node = comp.node;
let frame = comp._spriteFrame;
// Transform
let matrix = node._worldMatrix;
let a = matrix.m00, b = matrix.m01, c = matrix.m04, d = matrix.m05,
tx = matrix.m12, ty = matrix.m13;
ctx.transform(a, b, c, d, tx, ty);
ctx.scale(1, -1);
if (frame._rotated) {
ctx.rotate(- Math.PI / 2);
}

// TODO: handle blend function

// opacity
utils.context.setGlobalAlpha(ctx, node.opacity / 255);

let tex = comp._spriteFrame._texture,
let tex = frame._texture,
data = comp._renderData._data;

let image = utils.getColorizedImage(tex, node._color);
Expand Down
36 changes: 25 additions & 11 deletions cocos2d/core/renderer/canvas/renderers/sprite/sliced.js
Expand Up @@ -98,33 +98,47 @@ let renderer = {
sizableWidth = sizableWidth < 0 ? 0 : sizableWidth;
sizableHeight = sizableHeight < 0 ? 0 : sizableHeight;

data[0].x = -appx;
data[0].y = -appy;
data[1].x = leftWidth * xScale - appx;
data[1].y = bottomHeight * yScale - appy;
data[2].x = data[1].x + sizableWidth;
data[2].y = data[1].y + sizableHeight;
data[3].x = width - appx;
data[3].y = height - appy;

if (frame._rotated) {
data[0].y = -appx;
data[0].x = -appy;
data[1].y = rightWidth * xScale - appx;
data[1].x = bottomHeight * yScale - appy;
data[2].y = data[1].y + sizableWidth;
data[2].x = data[1].x + sizableHeight;
data[3].y = width - appx;
data[3].x = height - appy;
} else {
data[0].x = -appx;
data[0].y = -appy;
data[1].x = leftWidth * xScale - appx;
data[1].y = bottomHeight * yScale - appy;
data[2].x = data[1].x + sizableWidth;
data[2].y = data[1].y + sizableHeight;
data[3].x = width - appx;
data[3].y = height - appy;
}

renderData.vertDirty = false;
},

draw (ctx, comp) {
let node = comp.node;
let frame = comp._spriteFrame;
// Transform
let matrix = node._worldMatrix;
let a = matrix.m00, b = matrix.m01, c = matrix.m04, d = matrix.m05,
tx = matrix.m12, ty = matrix.m13;
ctx.transform(a, b, c, d, tx, ty);
ctx.scale(1, -1);

if (frame._rotated) {
ctx.rotate(- Math.PI / 2);
}
// TODO: handle blend function

// opacity
utils.context.setGlobalAlpha(ctx, node.opacity / 255);

let tex = comp._spriteFrame._texture,
let tex = frame._texture,
data = comp._renderData._data;

let image = utils.getColorizedImage(tex, node._color);
Expand Down