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 image optimizations #325

Merged
merged 3 commits into from
Nov 24, 2020
Merged
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
95 changes: 76 additions & 19 deletions src/js/Templates/Shared/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ class Image extends React.Component {
constructor(props) {
super(props);
this.state = {error: false, refreshed: false};
this.canvasStyle = {
height: null,
width: null,
scaledDimensions: null,
x: null,
y: null
};
}

scaleImage(ogDimension, parentDimension) {
Expand All @@ -30,28 +37,45 @@ class Image extends React.Component {
const canvas = this.refs.canvas
const ctx = canvas.getContext("2d")
const img = this.refs.image
canvas.width = canvasContainer.clientWidth
canvas.height = canvasContainer.clientHeight

if (this.canvasStyle.height && this.canvasStyle.width) {
canvas.width = this.canvasStyle.width
canvas.height = this.canvasStyle.height
} else {
canvas.width = canvasContainer.clientWidth
canvas.height = canvasContainer.clientHeight
this.canvasStyle.width = canvasContainer.clientWidth
this.canvasStyle.height = canvasContainer.clientHeight
}

img.onload = () => {
var scaledDimensions = this.scaleImage({
"width": img.width,
"height": img.height
}, {
"width": canvas.width,
"height": canvas.height
}
);
var scaledDimensions;
if (this.canvasStyle.scaledDimensions) {
scaledDimensions = this.canvasStyle.scaledDimensions
} else {
scaledDimensions= this.scaleImage({
"width": img.width,
"height": img.height
}, {
"width": this.canvasStyle.width,
"height": this.canvasStyle.height
}
);
}

ctx.clearRect(0, 0, this.canvasStyle.width, this.canvasStyle.height);

ctx.clearRect(0, 0, canvasContainer.clientWidth, canvasContainer.clientHeight);
if (!this.canvasStyle.x || !this.canvasStyle.y) {
this.canvasStyle.x = (canvas.width / 2) - (scaledDimensions.width / 2);
this.canvasStyle.y = (canvas.height / 2) - (scaledDimensions.height / 2);
}

var x = (canvas.width / 2) - (scaledDimensions.width / 2);
var y = (canvas.height / 2) - (scaledDimensions.height / 2);
ctx.drawImage(img, x, y, scaledDimensions.width, scaledDimensions.height);
ctx.drawImage(img, this.canvasStyle.x, this.canvasStyle.y, scaledDimensions.width, scaledDimensions.height);

ctx.globalCompositeOperation = "source-atop";
ctx.globalAlpha = 1.0;
ctx.fillStyle = fillColor;
ctx.fillRect(0, 0, canvasContainer.clientWidth, canvasContainer.clientHeight);
ctx.fillRect(0, 0, this.canvasStyle.width, this.canvasStyle.height);

ctx.globalCompositeOperation = "source-over";
ctx.globalAlpha = 1.0;
Expand All @@ -69,6 +93,30 @@ class Image extends React.Component {
this.drawImage();
}

shouldComponentUpdate(nextProps, nextState) {
// Check if any props changed
if (this.props.fillColor != nextProps.fillColor) {
return true;
}
if (this.props.image != nextProps.image) {
return true;
}
if (this.props.isTemplate != nextProps.isTemplate) {
return true;
}
if (this.props.class != nextProps.class) {
return true;
}

// Check if next image is to be refreshed
if (nextProps.refreshImage == nextProps.image) {
return true;
}

// No updates required
return false;
}

onError(event) {
const state = store.getState();
const activeApp = state.activeApp;
Expand Down Expand Up @@ -96,10 +144,19 @@ class Image extends React.Component {
if(this.props.image && !this.state.error) {
if(this.props.isTemplate) {
var hidden = {display:'none'};
var size = {
height: "100%",
width: "100%"
var size;
if (this.canvasStyle.height && this.canvasStyle.width) {
size = {
height: this.canvasStyle.height,
width: this.canvasStyle.width
}
} else {
size = {
height: "100%",
width: "100%"
}
}

return (
<div style={size} ref="canvasContainer">
<canvas ref="canvas" className={this.props.class}/>
Expand Down Expand Up @@ -137,4 +194,4 @@ const mapStateToProps = (state) => {

export default connect(
mapStateToProps
)(Image)
)(Image)