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

Rotate image #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/CanvasUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ function drawImage (ctx, image, x, y, width, height, options) {
dx = Math.round(x);
dy = Math.round(y);

ctx.drawImage(image.getRawImage(), sx, sy, sw, sh, dx, dy, dw, dh);
if (options.rotate) {
ctx.save();
ctx.translate(dx + (dw / 2), dy + (dh / 2));
ctx.rotate(options.rotate * Math.PI / 180);
ctx.drawImage(image.getRawImage(), sx, sy, sw, sh, - (dw / 2), - (dh / 2), dw, dh);
ctx.restore();
} else {
ctx.drawImage(image.getRawImage(), sx, sy, sw, sh, dx, dy, dw, dh);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/DrawingUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ function drawImageRenderLayer (ctx, layer) {
return;
}

CanvasUtils.drawImage(ctx, image, layer.frame.x, layer.frame.y, layer.frame.width, layer.frame.height);
CanvasUtils.drawImage(ctx, image, layer.frame.x, layer.frame.y, layer.frame.width, layer.frame.height, {rotate: layer.rotate});
}

/**
Expand Down
7 changes: 5 additions & 2 deletions lib/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var RawImage = createComponent('Image', LayerMixin, {

layer.type = 'image';
layer.imageUrl = props.src;
layer.rotate = props.rotate;
},

mountComponent: function (rootID, transaction, context) {
Expand All @@ -33,6 +34,7 @@ var RawImage = createComponent('Image', LayerMixin, {
var prevProps = this._currentElement.props;
var props = nextComponent.props;
this.applyLayerProps(prevProps, props);
this.applyImageProps(prevProps, props);
this._currentElement = nextComponent;
this.node.invalidateLayout();
},
Expand All @@ -46,7 +48,8 @@ var Image = React.createClass({
style: React.PropTypes.object,
useBackingStore: React.PropTypes.bool,
fadeIn: React.PropTypes.bool,
fadeInDuration: React.PropTypes.number
fadeInDuration: React.PropTypes.number,
rotate: React.PropTypes.number
},

getInitialState: function () {
Expand Down Expand Up @@ -92,7 +95,7 @@ var Image = React.createClass({
return (
React.createElement(Group, {ref: 'main', style: style},
React.createElement(Layer, {ref: 'background', style: backgroundStyle}),
React.createElement(RawImage, {ref: 'image', src: this.props.src, style: imageStyle, useBackingStore: useBackingStore})
React.createElement(RawImage, {ref: 'image', src: this.props.src, style: imageStyle, useBackingStore: useBackingStore, rotate: this.props.rotate})
)
);
},
Expand Down