Fullscreen image in Canvas

2,268 Kb

How do I make an fullscreen image in canvas?

In a project, I needed to draw image fullscreen in canvas.. What is a fullscreen image in canvas? How do you make a fullscreen image in canvas? This script and codes were developed by Chrysto on 16 July 2022, Saturday.

Fullscreen image in Canvas Previews

Fullscreen image in Canvas - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Fullscreen image in Canvas</title> <script src="http://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
<body> <div class="canvas__holder"> <canvas id="image__canvas"> </canvas>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>

Fullscreen image in Canvas - Script Codes CSS Codes

body, html, div, canvas { margin: 0; padding: 0; outline: none; box-sizing: border-box;

Fullscreen image in Canvas - Script Codes JS Codes

$(function() { var $window = $(window), $canvasHolder = $(".canvas__holder"), $canvas = $("#image__canvas"), canvas = $canvas.get(0), canvasContext = canvas.getContext("2d") var winWidth, winHeight, winRatio, imgWidth, imgHeight, imgRatio, canvasInited; var img; var $img = $("<img />", { "src": "http://ivayloyovchev.com/pulsepro/data/img/gallery/personal/IMG_0010_.jpg" }).load(function() { img = $img.get(0); imgWidth = img.width; imgHeight = img.height; imgRatio = imgWidth / imgHeight; canvasInited = true; resizeCanvas(); }); $window.on("resize", onResize); $window.trigger("resize"); function onResize() { winWidth = $window.width(); winHeight = $window.height(); winRatio = winWidth / winHeight; $canvas.attr({ "width": winWidth + "px", "height": winHeight + "px" }); $canvasHolder.add($canvas).css({ "width": winWidth + "px", "height": winHeight + "px" }); if (canvasInited) { resizeCanvas(); } } function resizeCanvas() { var newWidth = winWidth, newHeight = winHeight, newX = 0, newY = 0; if (winRatio > imgRatio) { newHeight = Math.round(winWidth / imgRatio); newY = (winHeight - newHeight) / 2; } else { newWidth = Math.round(winHeight * imgRatio); newX = (winWidth - newWidth) / 2; } canvasContext.drawImage(img, newX, newY, newWidth, newHeight); }
Fullscreen image in Canvas - Script Codes
Fullscreen image in Canvas - Script Codes
Home Page Home
Developer Chrysto
Username bassta
Uploaded July 16, 2022
Rating 3
Size 2,268 Kb
Views 30,360
Do you need developer help for Fullscreen image in Canvas?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Chrysto (bassta) Script Codes
Create amazing sales emails with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!