html canvas缩放抗锯齿,Canvas上绘制的文本的抗锯齿效果差

这段代码实现了一种颜色混合算法`alphaBlend`,用于结合背景色和前景色。`process`函数处理文本像素,使用给定的颜色进行混合。此外,`toColor`函数将颜色字符串转换为RGB值。在`renderOnce`函数中,该算法被应用于在两个canvas元素之间进行图像处理,展示颜色混合效果。
摘要由CSDN通过智能技术生成

function alphaBlend(gamma, c1, c2, alpha) {

c1 = c1/255.0;

c2 = c2/255.0;

var c3 = Math.pow(

Math.pow(c1, gamma) * (1 - alpha)

+ Math.pow(c2, gamma) * alpha,

1/gamma

);

return Math.round(c3 * 255);

}

function process(textPixels, destPixels, fg, bg) {

var gamma = 2.2;

for (var y = 0; y < textPixels.height; y ++) {

var history = [255, 255, 255];

var pixel_number = y * textPixels.width;

var component = 0;

for (var x = 0; x < textPixels.width; x ++) {

var alpha = textPixels.data[(y * textPixels.width + x) * 4 + 1] / 255.0;

alpha = Math.pow(alpha, gamma);

history[component] = alpha;

alpha = (history[0] + history[1] + history[2]) / 3;

out = alphaBlend(gamma, bg[component], fg[component], alpha);

destPixels.data[pixel_number * 4 + component] = out;

/* advance to next component/pixel */

component ++;

if (component == 3) {

pixel_number ++;

component = 0;

}

}

}

}

function toColor(colorString) {

return [parseInt(colorString.substr(1, 2), 16),

parseInt(colorString.substr(3, 2), 16),

parseInt(colorString.substr(5, 2), 16)];

}

function renderOnce() {

var phrase = "Corporate GOVERNANCE"

var c1 = document.getElementById("c1"); //the hidden canvas

var c2 = document.getElementById("c2"); //the canvas

var textSize=40;

var font = textSize+"px Arial"

var fg = "#ff0000";

var bg = "#fff9e1";

var ctx1 = c1.getContext("2d");

var ctx2 = c2.getContext("2d");

ctx1.fillStyle = "rgb(255, 255, 255)";

ctx1.fillRect(0, 0, c1.width, c1.height);

ctx1.save();

ctx1.scale(3, 1);

ctx1.font = font;

ctx1.fillStyle = "rgb(255, 0, 0)";

ctx1.fillText(phrase, 0, textSize);

ctx1.restore();

var textPixels = ctx1.getImageData(0, 0, c1.width, c1.height);

var colorFg = toColor(fg);

var colorBg = toColor(bg);

var destPixels3 = ctx1.getImageData(0, 0, c1.width, c1.height);

process(textPixels, destPixels3, colorBg, colorFg);

ctx2.putImageData(destPixels3, 0, 0);

//for comparison, show Comparison Text without anti aliaising

ctx2.font = font;

ctx2.fillStyle = "rgb(255, 0, 0)";

ctx2.fillText(phrase, 0, textSize*2);

};

renderOnce();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值