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

Fix issues with updating template images #338

Merged
merged 5 commits into from
Jan 14, 2021
Merged
Changes from 4 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
91 changes: 58 additions & 33 deletions src/js/Templates/Shared/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ class Image extends React.Component {
constructor(props) {
super(props);
this.state = {error: false, refreshed: false};
this.canvasStyle = {
this.customCanvasStyle = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JackLivio can you clarify the purpose of this variable? Is the intention that someone who adopts the generic HMI will modify the customCanvasStyle value here in the constructor to hardcode canvas sizes for the whole HMI?

This variable currently has no effect on how an Image is rendered.

Copy link
Collaborator

@Jack-Byrne Jack-Byrne Jan 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iCollin Referring to canvasStyle before the changes in the pr, that object was created to store the canvasContainer height and width. There was a strange issue in safari where the canvasContainer height would increase when the image in a button that was clicked on, and it would drive the image down the page.

height: null,
width: null,
scaledDimensions: null,
x: null,
y: null
};
this.canvasDimensions = {
height: null,
width: null
};
this.initialCanvasDimensions = {
height: null,
width: null
};
}

scaleImage(ogDimension, parentDimension) {
Expand All @@ -23,7 +31,7 @@ class Image extends React.Component {
}
var widthRatio = parentDimension.width / ogDimension.width
var heightRatio = parentDimension.height / ogDimension.height
var minScale = Math.min(widthRatio , heightRatio)
var minScale = Math.min(widthRatio, heightRatio)
scaledDimensions.width = ogDimension.width * minScale
scaledDimensions.height = ogDimension.height * minScale

Expand All @@ -37,51 +45,64 @@ class Image extends React.Component {
const canvas = this.refs.canvas
const ctx = canvas.getContext("2d")
const img = this.refs.image

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
if (!this.initialCanvasDimensions.height || !this.initialCanvasDimensions.width) {
this.initialCanvasDimensions.width = canvasContainer.clientWidth
this.initialCanvasDimensions.height = canvasContainer.clientHeight
}

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

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

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, y;
if (this.customCanvasStyle.x && this.customCanvasStyle.y) {
x = this.customCanvasStyle.x;
y = this.customCanvasStyle.y;
}
else {
x = (canvas.width / 2) - (scaledDimensions.width / 2);
y = (canvas.height / 2) - (scaledDimensions.height / 2);
}

ctx.drawImage(img, this.canvasStyle.x, this.canvasStyle.y, scaledDimensions.width, scaledDimensions.height);
ctx.drawImage(img, x, y, scaledDimensions.width, scaledDimensions.height);

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

ctx.globalCompositeOperation = "source-over";
ctx.globalAlpha = 1.0;
}
//Refresh image to trigger onload
img.src = this.props.image;
img.src = this.props.image + "?m=" + new Date().getTime();
}
}

Expand All @@ -95,21 +116,25 @@ class Image extends React.Component {

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

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

if (nextState.error) {
return true;
}

Expand Down Expand Up @@ -142,13 +167,13 @@ class Image extends React.Component {

render() {
if(this.props.image && !this.state.error) {
if(this.props.isTemplate) {
if (this.props.isTemplate) {
var hidden = {display:'none'};
var size;
if (this.canvasStyle.height && this.canvasStyle.width) {
if (this.customCanvasStyle.height && this.customCanvasStyle.width) {
size = {
height: this.canvasStyle.height,
width: this.canvasStyle.width
height: this.customCanvasStyle.height,
width: this.customCanvasStyle.width
}
} else {
size = {
Expand All @@ -163,7 +188,7 @@ class Image extends React.Component {
<img
ref="image"
style={hidden}
src={this.props.image}
src={this.props.image + "?m=" + new Date().getTime()}
alt="SDL_Image"
onError={e => this.onError(e)}
/>
Expand Down