Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to set canvas height and width dynamically

var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');

function drawTextAndResize(text, fontSize, font){
  text = text || "hello world";
  fontSize =  fontSize || 48;
  font = font || "serif";
  // First we set the font to the context
  ctx.font = fontSize + 'px '+ font;
  // We set the size of our canvas accordingly to the width of our text
  canvas.width = ctx.measureText(text).width;
  // 1.3*fontSize is an approximation of the line height, for a complete way to get the height, refer to this answer : http://stackoverflow.com/a/17631567/3702797
  canvas.height = fontSize*1.3;
  // Since our context has been reset, we have to reset its font as well
  ctx.font = fontSize + 'px '+ font;
  // Finally we draw the text, approximately vertically centered
  ctx.fillText(text, 0,canvas.height/1.3);
  }

drawTextAndResize("This is some text", 32, "arial");
setTimeout(drawTextAndResize, 4000);
Comment

How to set canvas height and width dynamically

const drawComponents = () => {
    	/* Code for drawing on the canvas */
    
    	// Return the animation interval.
    	return setInterval(() => {
    		/* Code for changes to canvas over time. */
    	}, 100);
    };
Comment

PREVIOUS NEXT
Code Example
Javascript :: convert javascript to java 
Javascript :: Parsing error: JSX value should be either an expression or a quoted JSX text. 
Javascript :: javascript get minutes between two dates 
Javascript :: Just allow Intergers in Input Field 
Javascript :: port for sqlexpress not found in desktop node.js 
Javascript :: nextjs youtube embed 
Javascript :: formatting time for ical export 
Javascript :: Minimum Path Sum for loop 
Javascript :: Bootstrap 5 data attributes different from Bootstrap 4 
Javascript :: js button that starts programe 
Javascript :: javascript splice method 
Javascript :: row no datatable 
Javascript :: prime number in javascript using for loop 
Javascript :: addeve 
Javascript :: javascript python comparison 
Javascript :: react-bootstrap-sweetalert is not running 
Javascript :: Check if the same text is repeated javascript todo-app 
Javascript :: tempusdominus calendar out of background size 
Javascript :: js execute after running the html file 
Javascript :: extract image in p5.js 
Javascript :: plumsail on change event value 
Javascript :: mongoose auto delete after time 
Javascript :: How to lock thread in javascript energy efficient 
Javascript :: getting xml from response, make sure server returns valid xml and the "content-type" header is set 
Javascript :: AngularJS SPA edit button function 
Javascript :: set default value in dynamic dropdown angularjs 
Javascript :: Syntax for npx 
Javascript :: on veiwport reveal javascript 
Javascript :: Node.js and Express session handling - Back button problem 
Javascript :: get range of items in list javascript react native 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =