Canvas is a new tag of HTML5. It is a canvas that can draw graphics. The default size of the canvas is 300x150. There are issues to pay attention to when customizing the size of the drawing canvas, which is when using styles to set the height and width For example,
<div> <canvas>您的浏览器不支持H5画布属性</canvas> <script> var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.arc(120, 75, 20, 0, Math.PI * 2, false); ctx.fillStyle = "#000"; ctx.fill(); </script> </div>
is equivalent to stretching the entire canvas, and drawing like this The resulting graphics are blurry.
You can see that the edge of the circle is blurred and turned into an ellipse? This is because the canvas is still the default size of 300px wide and 150px high, but the canvas is forcibly stretched to 1000x600 using style. The width is expanded by 3.33 times and the height is expanded by 4 times, so it becomes an ellipse. Change the width to 1200 and it will be round.
So you cannot set the size in the style. You should use the width and height properties of the canvas to set the height. Look at the code below. Note that the parameters for drawing a circle have also changed
<div> <canvas>您的浏览器不支持H5画布属性</canvas> <script> var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.arc(500, 300, 200, 0, Math.PI * 2, false); ctx.fillStyle = "#000"; ctx.fill(); </script> </div>
So to set the size of the canvas, use the width and height attributes that come with the canvas. It is the real size of the canvas. There are ways to solve it online, but I didn't try it because it seemed too troublesome. It would be better to just give it a fixed size in the future. Some people say how to customize it like this. It's easy. Just put a div and get the width in js or get the width and height of the screen and set the value of the canvas. code show as below.
<div style="width:1000px; height: 600px; " id="canvas_size"><canvas id="canvas" style="background-color: #eee">您的浏览器不支持H5画布属性</canvas> <script type="text/javascript">var canvas = document.getElementById("canvas");var canvas_size = document.getElementById("canvas_size");//获取divvar ctx = canvas.getContext("2d");canvas.width = canvas_size.offsetWidth;//设置宽canvas.height = canvas_size.offsetHeight;//设置高ctx.arc(500, 300, 200, 0, Math.PI * 2, false);ctx.fillStyle = "#000";ctx.fill();</script> </div>
The result is the same as the picture above, you can try it yourself.
Summary
##The width and height of the canvas must be written using the built-in width and height to determine the true canvas size, <canvas width="1000px" height="600px" style="background-color: #eee">Your browser does not support H5 canvas attributes</canvas><span style="font-size: 16px;"></span>
, adaptive use js to set.
The above is the detailed content of How to solve the problem of blurry images drawn on Canvas?. For more information, please follow other related articles on the PHP Chinese website!

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
