HTML5 画布抗锯齿?

HTML5 canvas anti-alias?(HTML5 画布抗锯齿?)
本文介绍了HTML5 画布抗锯齿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用画布绘制二次曲线.这是代码:
HTML:

I'm trying to draw a quadratic curve with canvas. Here is the code:
HTML:

<canvas id="mycanvas"> 
    Your browser is not supported.
</canvas> 

JavaScript:

JavaScript:

var canvas = document.getElementById("mycanvas");
canvas.style.width = "1000px";
canvas.style.height = "1000px";
if (canvas.getContext) {
    var ctx = canvas.getContext("2d");
    var x = 0,
        y = 0;
    setInterval(function() {
        ctx.lineTo(x, y);
        ctx.stroke();
        x += 1;
        y = 0.01 * x * x;
    }, 100);
}

但是结果真的很丑,第一,线条太粗,第二,别名太明显了....我该如何改善呢?
你可以在这里看到效果:http://jsfiddle.net/7wNmx/1/

But the result is really ugly, first, the lines are too thick, second, the alias is so obvious.... how could I improve it?
You can see the effect here: http://jsfiddle.net/7wNmx/1/

推荐答案

您正在做的是创建一个默认大小为 300 x 150 的画布,然后使用 CSS 将其放大到 1000 x 1000 像素.但是像这样放大它只会放大像素的大小,并不会增加画布本身的分辨率.您需要做的是使用 widthheight 属性设置画布本身的实际尺寸:

What you're doing is creating a canvas which is the default size, 300 by 150, and then scaling it up using CSS to 1000px by 1000px. But scaling it up like that just magnifies the size of the pixels, it doesn't increase the resolution of the canvas itself. What you need to do is set the actual dimensions of the canvas itself using the width and height attributes:

<canvas width="1000" height="1000" id="mycanvas"> 
    Your browser is not supported.
</canvas>

那么你就不需要再设置canvas.style.widthheight了.

Then you don't need to scale it up by setting canvas.style.width and height any more.

这篇关于HTML5 画布抗锯齿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)