0

a=(canvas.width-70)/2 how is the value became 115 canvas is 900px by 500px is there a logic or a rule in the javascript canvas

10th Jul 2018, 3:18 AM
nard Lee
nard Lee - avatar
11 odpowiedzi
+ 2
Your code?
10th Jul 2018, 3:19 AM
Calviղ
Calviղ - avatar
+ 2
where did you specify your canvas width?
10th Jul 2018, 3:35 AM
Calviղ
Calviղ - avatar
+ 2
Canvas width and height are better use inline attribute to set
10th Jul 2018, 4:10 AM
Calviղ
Calviղ - avatar
+ 1
Try using canvas.style.width canvas.width is the HTML attribute. canvas.style.width is the CSS property. The default width attribute of a canvas element is typically 300px. (300 - 70)/2 = 115 So that's why you get 115. Does this fix the problem? Because I know something else you might try.
10th Jul 2018, 4:05 AM
James
James - avatar
0
<html> <canvas id="canvas"></canvas> <script> a = document.getElementById("canvas"); var b =(a.width - 70) /2; document.write(b, "<br>"); document.write(a.width); </script> </html>
10th Jul 2018, 3:27 AM
nard Lee
nard Lee - avatar
0
well this is funny.. but i did it in css
10th Jul 2018, 3:59 AM
nard Lee
nard Lee - avatar
0
oh...it looks better when I put the width and height inside the canvas tag
10th Jul 2018, 4:04 AM
nard Lee
nard Lee - avatar
0
haha I noticed that a while ago....thanks..
10th Jul 2018, 4:09 AM
nard Lee
nard Lee - avatar
0
So it's working now?
10th Jul 2018, 4:09 AM
James
James - avatar
0
no...im just trying to study some code for better understanding of the logic
10th Jul 2018, 4:18 AM
nard Lee
nard Lee - avatar
0
Its better add that set width and height like html attribute its DIFFERENT on set in css on canvas. If you set only css width/height you will get desidered size on screen BUT it will be resized from 300x150 and you will get sometimes a distorted canvas content. In practice, if do you not want distorted content, set width/height in html
10th Jul 2018, 7:33 AM
KrOW
KrOW - avatar