/    Sign up×
Community /Pin to ProfileBookmark

Canvas size and positioning based on client screen

Hi, I’ve created a script that retrieves the client’s screen width and height, based on which the script sets the canvas size and then repositions it to middle of the screen. Of all the functions, Function init and findInternetExplorer are recognised when using firefox web console however the remainder of the function come as undefined. However, when you call function calculateScreenCenteredPosition(clientScreenWidth, clientScreenHeight, designCanvas) it actually centers the canvas on the screen but using what size, I have no idea. The most recent function worked on is the setCanvasSize(clientScreenWidth, clientScreenHeight) and I have a feeling its something to do with this.

JavaScript:

var designCanvas, canvasContext;
var posNewX, posNewY;
var clientScreenWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var clientScreenHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

function init() {
designCanvas = document.getElementById(‘myDesign’);
canvasContext = designCanvas.getContext(‘2d’);
if ((designCanvas != null) && (canvasContext != null)) {
setCanvasSize(clientScreenWidth, clientScreenHeight);
}
}

function setCanvasSize(clientScreenWidth, clientScreenHeight) {
if (parseInt(clientScreenWidth, 2) > 1920) {
if ( findInternetExplorer() > 0 || navigator.userAgent.indexOf(‘Edge’) >= 0) {
if (parseInt(clientScreenWidth, 2) > 2400) {

} /* closing of greater > 2400 */
/* 22 & 21.5 Inch on Ms Edge and IE11 */
else if (parseInt(clientScreenWidth, 2) == 2400 && (parseInt(clientScreenHeight, 2) == 1204 || parseInt(clientScreenHeight, 2) > 1204) || parseInt(clientScreenWidth, 2) == 2259 && (parseInt(clientScreenHeight, 2) >= 1333 && parseInt(clientScreenHeight, 2) <= 1271 || parseInt(clientScreenHeight, 2) > 1271) || parseInt(clientScreenWidth, 2) == 1976 && (parseInt(clientScreenHeight, 2) >= 1098 && parseInt(clientScreenHeight, 2) <= 1124 || parseInt(clientScreenHeight, 2) > 1124)) {
if (parseInt(clientScreenWidth, 2) == 2400 && (parseInt(clientScreenHeight, 2) == 1204 || parseInt(clientScreenHeight, 2) > 1204)) {
if (parseInt(clientScreenWidth, 2) == 2400 && parseInt(clientScreenHeight, 2) == 1204) {

} else {

}
} else if (parseInt(clientScreenWidth, 2) == 2259 && (parseInt(clientScreenHeight, 2) >= 1333 && parseInt(clientScreenHeight, 2) <= 1271 || parseInt(clientScreenHeight, 2) > 1271)) {
if (parseInt(clientScreenWidth, 2) == 2259 && (parseInt(clientScreenHeight, 2) >= 1333 && parseInt(clientScreenHeight, 2) <= 1271)) {

} else {

}
} else {

}
}

} /* closing of IE 11 and Edge If Cause */

} /* clientScreenWidth > 1920 */
else if (parseInt(clientScreenWidth, 2) == 1920 && ((parseInt(clientScreenHeight, 2) >= 935 && parseInt(clientScreenHeight, 2) <= 985) || parseInt(clientScreenHeight, 2) > 985) || parseInt(clientScreenWidth, 2) == 1680 && ((parseInt(clientScreenHeight, 2) >= 916 && parseInt(clientScreenHeight, 2) <= 949) || parseInt(clientScreenHeight, 2) > 949)) {
if (parseInt(clientScreenWidth, 2) == 1920 && ((parseInt(clientScreenHeight, 2) >= 935 && parseInt(clientScreenHeight, 2) <= 985) || parseInt(clientScreenHeight, 2) > 985)) {
if (parseInt(clientScreenWidth, 2) == 1920 && (parseInt(clientScreenHeight, 2) >= 935 && parseInt(clientScreenHeight, 2) <= 985)) {
designCanvas.width = 1175;
designCanvas.height = 825;
calculateScreenCenter(clientScreenWidth, clientScreenHeight, designCanvas);
} else {
designCanvas.width = 1175;
designCanvas.height = 920;
calculateScreenCenter(clientScreenWidth, clientScreenHeight, designCanvas);
}
} else {
if (parseInt(clientScreenWidth, 2) == 1680 && (parseInt(clientScreenHeight, 2) >= 916 && parseInt(clientScreenHeight, 2) <= 949)) {
designCanvas.width = 1075;
designCanvas.height = 725;
calculateScreenCenter(clientScreenWidth, clientScreenHeight, designCanvas);
} else {

}
}

}

} /* closing brace of function */

function calculateScreenCenter(clientScreenWidth, clientScreenHeight, designCanvas) {
var canvasWidth = parseInt(designCanvas.width, 2);
var canvasHeight = parseInt(designCanvas.height, 2);
var availableScreenWidth = parseInt(clientScreenWidth, 2);
var availableScreenHeight = parseInt(clientScreenHeight, 2);
if (((typeof clientScreenWidth === ‘string’) && (typeof clientScreenHeight === ‘string’)) | (typeof clientScreenWidth === ‘string’) | (typeof clientScreenHeight === ‘string’)) {
if (((typeof designCanvas.width === ‘string’) && (typeof designCanvas.height === ‘string’)) | (typeof designCanvas.width === ‘string’) | (typeof designCanvas.height === ‘string’)) {
posNewX = ((availableScreenWidth – canvasWidth) / 2) + ‘px’;
posNewY = ((availableScreenHeight – canvasHeight) / 2) + ‘px’;
repositionCanvas(posNewX, posNewY);
} else {
posNewX = ((availableScreenWidth – designCanvas.width) / 2) + ‘px’;
posNewY = ((availableScreenHeight – designCanvas.height) / 2) + ‘px’;
repositionCanvas(posNewX, posNewY);
}
} else {
posNewX = ((clientScreenWidth – designCanvas.width) / 2) + ‘px’;
posNewY = ((clientScreenHeight – designCanvas.height) / 2) + ‘px’;
repositionCanvas(posNewX, posNewY);
}
}

function repositionCanvas(posNewX, posNewY) {
designCanvas.style.marginLeft = posNewX;
designCanvas.style.marginTop = posNewY;
}

function findInternetExplorer() {
var ua = window.navigator.userAgent;
var Ind = ua.indexOf(“MSIE”);
if (Ind > 0) {
return parseInt(ua.substring(Ind + 5, ud.indexOf(“.”, Ind)));
} else if (!!navigator.userAgent.match(/Trident/7./)) {
return 11;
} else {
return 0;
}
}

window.onload = init;

HTML:

<canvas id=’myDesign’ width=’900′ height=’625′ style=’border:1px solid #D0D0D0;’> </canvas>

any ideas/ solutions would be much appreciated as would any ideas on how to compact or shorten the script and also to detect IE 11 without the use of function findInternetExplorer even though it works perfectly

Many Thanks

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliJul 20.2016 — There may be other issues, but first thing that I notice was that your code doesn't at every resizing of window. First implement window.onresize so that it calls setCanvasSize every time when window size is changed
Copy linkTweet thisAlerts:
@rootJul 21.2016 — Please post code using forum tags to separate question from code, make reading code easier and also highlight the part of the code that is causing your error messages in the console.
×

Success!

Help @nsath spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...