Resize Canvas Using HTML Slider

The HTML 5 Canvas has to be one of the coolest features that has been introduced in quite some time. Solid and well built, it makes image editing and modification such a breeze compared to the javascript-based tools we had in the past. There are bound to be some useful wrapper libraries out there that make the use of the canvas much easier, but for those looking to write code using the raw framework, there are some things that can be a little more tricky to achieve. One problem I had recently was being able to resize the canvas and the image inside of it. I technically wanted to zoom in and out, but the basic concept but it was essentially a resizing of the canvas.

The canvas library does contain a scale method, but it was not working the way I had wanted. I wanted to give users full control over how to resize the image. This meant, modifying height and width and then sliding the “zoom” button to increase or decrease the size of the image. I was able to achieve this, so I said I would make a post about it for others to hopefully find some use in.

So to start, here are some basic input controls. A box for the height and width and a slider then to zoom.

<p>Slide to scale canvas</p>
<input type="range" id="canvasscale" min="-10" max="20" value="0" onchange="scaleCanvas()"><br>
<p>Rezize canvas using input controls</p>
Height:<input type="text" id="height" onchange="resizeCanvas()"> Width: <input type="text" id="width" onchange="resizeCanvas()">

There are onchange events tied to these inputs. When the height or width is changed, call a function to actually take those values and resize the canvas. The scale function will simply take the value from the slider and perform a calculation to get the new height and width. You can set these however you wish. If the canvas is to be populated with a single image, then you can set these values from the dimensions of this image.

So all you need now is the canvas and the javascript code to resize the HTML canvas when the user modifies the height, width or moves the slider to zoom in or out of the image. The canvas is actually modified with this resizing so if you save it, the resulting image will be the same dimensions as the values in the input boxes.

<hr>
<canvas id="testcanvas" style="background-color:black;display:block"></canvas>

<script>
	originalHeight = 200;
	originalWidth = 400;
	function scaleCanvas()
	{
		var scale = document.getElementById("canvasscale").value;
		
		document.getElementById("height").value = Math.round(((originalHeight * ((scale * 10) / 100)) + originalHeight) * 100) / 100;
		document.getElementById("width").value = Math.round(((originalWidth * ((scale * 10) / 100)) + originalWidth) * 100) / 100;
		resizeCanvas();
	}
	
	function resizeCanvas()
	{
		var canvas = document.getElementById("testcanvas");
		var context = canvas.getContext("2d");	
		
		canvas.height = document.getElementById("height").value;
		canvas.width = document.getElementById("width").value;
	}
</script>

If you are dealing with a canvas that contains one image and you want to ensure that the image remains the same height and width as the canvas as it is modified, then you can use the following line of code to very easily do this. You should call this inside the resizeCanvas function. You will, of course, have to store the selected image as a global variable so as not to lose it when the canvas is modified. You will need something more complex if you are dealing with many different images and shapes on the canvas.

function resizeCanvas()
{
	var canvas = document.getElementById("testcanvas");
	var context = canvas.getContext("2d");	
	
	canvas.height = document.getElementById("height").value;
	canvas.width = document.getElementById("width").value;
	context.drawImage(selectedimage, 0, 0, canvas.width, canvas.height);
}

Related Articles

Related Questions

Function Keys Reversing Between Fn Actions And Normal

My keyboard has the usual F1 to F12 keys along the top. I use these for shortcuts in various applications. These keys also have...

Whirlpool Oven F6E6: Appliance Manager 1 Board Communication

I have a brand new Whirlpool oven W11I OM1 4MS2 H or (859991549450). I bought it alongside the microwave combi oven. I have had...

Whats the difference between the Tapo P100 and the P105?

There are a few different Tapo smart plugs. The P100 and P110 differ based on the smart power monitoring feature but where does the...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

AI Content Detector

We've got this awesome free tool that'll help you figure out if that content you're looking at was written by a human or some...

Image Saturation

Are you looking for an easy-to-use, free app to modify your image saturation levels and make your pictures truly pop? Look no further! Our...

Pixelate Image Tool

Introducing the ultimate free online image pixelator tool that allows you to easily transform your images into stunning pixel art in just a few...

Image RGB Level Adjustment Tool

Introducing the ultimate image color adjustment tool for all your photo editing needs. Our free online tool lets you take full control of your...

Image Color Inverter

Looking for a quick and efficient way to convert your images into negatives? Our Free Image to Negative Converter is the answer! Our online...

Negative Image to Color Image Converter

Welcome to our Negative Image to Color Image Converter, a free and easy-to-use tool that helps you convert your old negative images into vibrant,...

Latest Posts

Latest Questions