- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建在 HiDPI 设备上看起来不模糊的动画弧线。
这是我的弧线在 iPhone 5s 上的样子:
您可以看到接近 0、90、180 度的弧度变得太尖锐了。我怎样才能避免这种情况?
这是我的代码:
// Canvas arc progress
const can = document.getElementById('canvas');
const ctx = can.getContext('2d');
const circ = Math.PI * 2;
const quart = Math.PI / 2;
const canvasSize = can.offsetWidth;
const halfCanvasSize = canvasSize / 2;
let start = 0,
finish = 70,
animRequestId = null;
// Get pixel ratio
const ratio = (function() {
const dpr = window.devicePixelRatio || 1,
bsr = ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio || 1;
return dpr / bsr;
})();
// Set canvas h & w
can.width = can.height = canvasSize * ratio;
can.style.width = can.style.height = canvasSize + 'px';
ctx.scale(ratio, ratio)
ctx.beginPath();
ctx.strokeStyle = 'rgb(120,159,194)';
ctx.lineCap = 'square';
ctx.lineWidth = 8.0;
ctx.arc(halfCanvasSize, halfCanvasSize, halfCanvasSize - 4, 0, circ, false);
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.strokeStyle = 'rgb(244,247,255)';
ctx.lineCap = 'round';
ctx.lineWidth = 8.0;
ctx.closePath();
let imd = ctx.getImageData(0, 0, canvasSize, canvasSize);
const draw = (current) => {
ctx.putImageData(imd, 0, 0);
ctx.beginPath();
ctx.arc(halfCanvasSize, halfCanvasSize, halfCanvasSize - 4, -(quart), ((circ) * current) - quart, false);
ctx.stroke();
};
(function animateArcProgress() {
animRequestId = requestAnimationFrame(animateArcProgress);
if (start <= finish) {
draw(start / 100);
start += 2;
} else {
cancelAnimationFrame(animRequestId);
}
})();
body {
margin: 0;
}
div {
display: flex;
align-items: center;
justify-content: center;
height: 300px;
widht: 300px;
background: #85b1d7;
}
canvas {
height: 250px;
width: 250px;
}
<div>
<canvas id='canvas'></canvas>
</div>
最佳答案
您可以按照下面的代码在内部绘制 1/2 像素来柔化边缘。
我分别在 8、7.5 和 7 像素宽度 alpha 颜色值 0.25、0.5 和 1 处渲染了弧线 3 次。
你可以把它做成你想要的软度。
顺便说一句,使用 putImageData 非常慢,为什么不直接将背景渲染到另一个 Canvas 上并通过 ctx.drawImage(offScreencanvas,0,0) 绘制,这样您将使用 GPU 渲染背景而不是 CPU 通过图形端口总线。
我添加了更多代码来显示您可以获得的不同柔化效果,并添加了鼠标缩放,以便您可以更好地看到像素。
const can = document.getElementById('canvas');
const can2 = document.createElement("canvas"); // off screen canvas
const can3 = document.createElement("canvas"); // off screen canvas
const ctx = can.getContext('2d');
const ctx2 = can2.getContext('2d');
const ctx3 = can3.getContext('2d');
const circ = Math.PI * 2;
const quart = Math.PI / 2;
const canvasSize = can.offsetWidth;
const halfCanvasSize = canvasSize / 2;
const mouse = {x : null, y : null};
can.addEventListener("mousemove",function(e){
var bounds = can.getBoundingClientRect();
mouse.x = e.clientX - bounds.left;
mouse.y = e.clientY - bounds.top;
});
let start = 0,
finish = 70,
animRequestId = null;
// Get pixel ratio
const ratio = (function() {
const dpr = window.devicePixelRatio || 1,
bsr = ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio || 1;
return dpr / bsr;
})();
// Set canvas h & w
can2.height = can3.height = can2.width = can3.width = can.width = can.height = canvasSize * ratio;
can.style.width = can.style.height = canvasSize + 'px';
ctx.scale(ratio, ratio)
ctx2.scale(ratio, ratio)
ctx3.scale(ratio, ratio)
ctx2.beginPath();
ctx2.strokeStyle = 'rgb(120,159,194)';
ctx2.lineCap = 'square';
ctx2.lineWidth = 8.0;
ctx2.arc(halfCanvasSize, halfCanvasSize, halfCanvasSize - 4, 0, circ, false);
ctx2.stroke();
ctx2.closePath();
ctx2.beginPath();
ctx2.strokeStyle = 'rgb(244,247,255)';
ctx2.lineCap = 'round';
ctx2.lineWidth = 8.0;
ctx2.closePath();
const draw = (current) => {
ctx3.clearRect(0,0,canvas.width,canvas.height);
ctx3.drawImage(can2,0,0);
var rad = halfCanvasSize - 4;
const drawArc = () => {
ctx3.beginPath();
ctx3.arc(halfCanvasSize, halfCanvasSize, rad, -(quart), ((circ) * current) - quart, false);
ctx3.stroke();
}
// draw soft
ctx3.strokeStyle = 'rgb(244,247,255)';
ctx3.lineWidth = 8.5;
ctx3.globalAlpha = 0.25;
drawArc();;
ctx3.lineWidth = 7.0;
ctx3.globalAlpha = 0.5;
drawArc();;
ctx3.lineWidth = 6.5;
ctx3.globalAlpha = 1;
drawArc();
// draw normal
rad -= 12;
ctx3.lineWidth = 8.0;
ctx3.globalAlpha = 1;
drawArc();;
// draw ultra soft
rad -= 12;
ctx3.strokeStyle = 'rgb(244,247,255)';
ctx3.lineWidth = 9.0;
ctx3.globalAlpha = 0.1;
drawArc();
ctx3.lineWidth = 8.0;
ctx3.globalAlpha = 0.2;
drawArc();;
ctx3.lineWidth = 7.5;
ctx3.globalAlpha = 0.5;
drawArc();
ctx3.lineWidth = 6;
ctx3.globalAlpha = 1;
drawArc();
};
const zoomW = 30;
const zoomAmount = 5;
const drawZoom = () => {
ctx.drawImage(can3,0,0);
var width = zoomW * zoomAmount;
var cx = mouse.x - width / 2;
var cy = mouse.y - width / 2;
var c1x = mouse.x - zoomW / 2;
var c1y = mouse.y - zoomW / 2;
ctx.strokeStyle = 'rgb(244,247,255)';
ctx.lineWidth = 4;
ctx.strokeRect(cx,cy,width,width);
ctx.clearRect(cx,cy,width,width);
ctx.imageSmoothingEnabled = false;
ctx.mozImageSmoothingEnabled = false;
ctx.drawImage(can3,c1x,c1y,zoomW,zoomW,cx,cy,width,width);
ctx.imageSmoothingEnabled = true;
ctx.mozImageSmoothingEnabled = true;
}
function keepUpdating(){
ctx.clearRect(0,0,can.width,can.height);
drawZoom();
requestAnimationFrame(keepUpdating);
}
(function animateArcProgress() {
ctx.clearRect(0,0,can.width,can.height);
draw(start / 100);
drawZoom();
if (start <= finish) {
start += 0.5;
requestAnimationFrame(animateArcProgress);
} else {
requestAnimationFrame(keepUpdating);
}
})();
body {
margin: 0;
}
div {
display: flex;
align-items: center;
justify-content: center;
height: 300px;
widht: 300px;
background: #85b1d7;
}
canvas {
height: 250px;
width: 250px;
}
<div>
<canvas id='canvas'></canvas>
</div>
关于javascript - Retina 上的 Canvas 弧线在某些点上太尖锐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39355632/
我没有 Retina 设备,但我一直在网上看到这个讨论。我已阅读有关为配备 Retina 显示屏的设备提供 2 倍图像尺寸的解决方案,以确保位图不会变得模糊。 但是,我不明白这是如何工作的。首先,正如
根据我的研究,我了解到您想将图像的大小加倍以使其在视网膜显示器上看起来更清晰。 对于尺寸为500X500 px的图片,我想将它们存储为1000X1000。 如有错误请指正 现在的问题是,通过使用 Go
目前,我的应用程序中的所有 UIImageView 都设置为 scaleToFill 模式,仅使用一个视网膜图像而不设置任何非视网膜图像。我想在非视网膜显示器上,额外的分辨率将自行丢失。 问题:这种方
我有以下媒体查询,以选择 iPad 视网膜和更小的显示器。 @media only screen and (min-device-width : 768px) and (max-device-widt
在深入研究我的下一个项目之前,我正在寻求建议。我想支持 iOS 现在支持的所有各种分辨率(视网膜、非视网膜 iPhone 和所有 3 款 iPad)。 开发人员完成这项工作最常用的方法是什么?我看到过
有没有一种方法可以在 Windows 上模拟 Retina 显示屏来测试 HiDPI 显示屏(例如 Retina)的网站? 我在标准的 24"1920x1080 显示器上运行 Windows。昨晚我在
我遇到了麻烦。我正在使用带有 UIKit 的 cocos2d。 Cocos2d版本为1.0.1。 我已经按照文档中的建议将平铺 map 添加到项目中: http://www.cocos2d-iphon
有人可以告诉我 iPhone/iPad 和 iPhone(Retina)/iPad(Retina) 的理想图像分辨率是多少吗?我从苹果网站和维基百科上获取了这些测量值。见下文: 普通 iPhone 4
这是我第一次开发应用程序。 根据 Apple iOS 人机界面指南,UIBarButtonItem 图像尺寸约为 20 x 20 和约 40 x 40。 我知道20x20是针对iPhone4之前版本的
我正在使用 Assets 目录来管理我应用程序的所有图像,并且我“正常”加载这些图像 [UIImage imageNamed:@"my-image"]; 我想做的是从 Assets 目录加载图像的非视
这可能是一个小众问题,实际上更像是一个小问题,但是: 我可以让 Xcode 自动构建到 iPhone Retina 模拟器和 iPad 非 Retina 模拟器吗? 即使使用 27"iMac,Reti
我有一个问题可能听起来很奇怪,但作为一个新手,我猜 no question 是一个愚蠢的问题...... 我是否需要为所有四台设备使用单独的 Storyboard? 现在我有两个 Storyboard
我是 opengl es 2.0 开发的新手。我从屏幕截图中获得的 UIImage 在非视网膜设备(iphone 4 和 ipad)上看起来不错,但是当我从视网膜设备上获得屏幕截图时,它似乎放大了。这
我正在使用 XCode 4.5 版。对于每个 Xib,我可以选择大小为“Retina 3.5 全屏”或“Retina 4 全屏”。 我的疑问是:如果我选择 Retina 4 全屏,我的应用程序能否在旧
我正在使用最新的 xCode 5,我正在尝试为我的通用应用程序设置启动图像。当我选择 Launch images for iPhone 时,一切正常。但是现在,当我尝试为 iPad 选择启动图像时,x
今天,我更新了Xcode4.5,并在我的电脑上安装了Xcode4.3,安装后我打开Xcode4.5,当我想运行我的应用程序时,只有IOS Device,iPad 6.0模拟器和 iPhone 6.0
我正在使用模拟器为 iPhone 5 准备我的应用程序,我发现了一个奇怪的问题,在我的一个 View 中,我的几个按钮在 iPhone(Retina 4 英寸) 模拟器,但在 iPhone(Retin
我们的目标是模拟开发人员可以使用原生 iOS 应用执行的操作:即,使用基于单位的单一布局来适应 Retina 显示屏 (640x960) 和非 Retina 显示屏 (320x480)。 所有 iOS
这是我第一次提问,因为我是新来的。非常感谢您的回答!感谢您抽出时间。 使用Xcode 5.1.1 这是我的问题,在模拟器或实际设备上测试时,结果不同。请参阅食物图像上所附的屏幕截图。当我在模拟器iPh
我更新了一个旧应用程序,修复了一个小错误。该应用程序是使用 Xcode 5.1 编译的。(无法使用 Assets 目录或添加 iPhone 6 和 6 Plus 图像。)但是,当我尝试提交它进行审核时
我是一名优秀的程序员,十分优秀!