HTML5 Canvas translate(0.5,0.5) 不修复线条模糊 [英] HTML5 Canvas translate(0.5,0.5) not fixing line blurryness

查看:23
本文介绍了HTML5 Canvas translate(0.5,0.5) 不修复线条模糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读

解决方案

Retina &HiDPI 设备

查看您提供的图像.画布是显示器分辨率的一半.放大并查看X"上的像素,DOM 线为 1 像素,但仍然是该字母像素的两倍宽.这意味着画布被拉伸,模糊是由于双线性过滤.要么缩小选项卡,要么使用 Retina 或 HiDPI 显示屏.设置 canvas.width &canvas.height 为两倍,设置 canvas.style.width &canvas.style.height 到 DOM 像素.删除 ctx.translate 并添加 ctx.scale(2,2) 一切都会清楚.

放大图像

Reading related articles, I've tried forcing the canvas size in pixels, specifying half pixels for the paths and also context.translate(0.5) but my canvas lines are still blurry.

See Codepen.

const ctx = canvas.getContext("2d");
ctx.translate(0.5, 0.5);

/* Function to draw HTML5 canvas line */
const drawPath = (startX, startY, endX, endY) => {
  ctx.beginPath(); 
  ctx.lineWidth = "1";
  ctx.strokeStyle = "red"; 
  ctx.moveTo(startX, startY);
  ctx.lineTo(endX, endY);
  ctx.stroke();
};

Where am I going wrong and how can I make my lines crisp like the border around the boxes in the demo?

How it appears for me:

解决方案

Retina & HiDPI devices

Looking at the image you supplied. The canvas is half the resolution of the display. Zoom in and look at the pixels on the "X", The DOM line is 1px yet still twice a wide as the pixels on that letter. That means the canvas is being stretched and the blur is due to the bilinear filtering. Either you are zoomed out on the tab or you have a retina or HiDPI display. Set the canvas.width & canvas.height to twice what it is, set the canvas.style.width & canvas.style.height to DOM pixels. Remove the ctx.translate and add ctx.scale(2,2) and all things will be clear.

A zoom in on the image

这篇关于HTML5 Canvas translate(0.5,0.5) 不修复线条模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆