HTML5 Canvas 颜色区分
我正在尝试使用 HTML5 画布绘制弹出窗口。 弹出窗口由绘制箭头的画布和下面的 div 组成:
正如您可以清楚地看到,箭头的颜色比 div 的背景颜色深。
代码被注入到浏览器的每个选项卡中,并在选择文本时显示弹出窗口。
这是绘图代码:
var ctx = $('#popupCanvas')[0].getContext("2d");
ctx.moveTo(10, 0);
ctx.lineTo(0, 10);
ctx.lineTo(20, 10);
ctx.fillStyle = "rgba(50,50,50,0.9)";
ctx.fill();
div 的 CSS 是:
#popupMenu
{
line-height: 0px;
display:block;
background-color:rgba(50,50,50,0.9);
color:#ddd;
-webkit-box-shadow: 2px 2px 5px #555;
-webkit-border-radius:5px;
font-family: Century Gothic, sans-serif;
font-style: normal;
font-variant: normal;
font-weight: normal;
font-size:0px;
padding:5px;
width:345px;
}
如您所见,颜色相同,但渲染方式不同。
我做错了什么?
为什么会有差异?我在 chrome 15 下使用 Webkit。
Edit1
我忘了提及,弹出窗口作为注入的 javascript\css 运行。
添加 HTML 源代码:
<div id='popupContainer'>
<canvas id='popupCanvas' width='20' height='10'/>
<div id='popupMenu'>
</div>
</div>
和完整的 CSS:
#popupCanvas
{
position:relative;
margin:0px;
padding:0px;
}
#popupContainer
{
position:absolute;
z-index: 999999;
direction:ltr;
text-align:center;
}
#popupMenu
{
line-height: 0px;
display:block;
background-color:rgba(50,50,50,0.9);
color:#ddd;
-webkit-box-shadow: 2px 2px 5px #555;
-webkit-border-radius:5px;
font-family: Century Gothic, sans-serif;
font-style: normal;
font-variant: normal;
font-weight: normal;
font-size:0px;
padding:5px;
width:345px;
}
Edit2
这是用户选择后的完整代码:
this.onTextSelected = function (selText) {
selectedText = selText;
// check if we have value
var selection = window.getSelection();
var node = document.elementFromPoint(currentMousePos.x, currentMousePos.y);
if (node.value) {
rect = getTextBoundingRect(node, $(node).caret().start, $(node).caret().end);
}
else
rect = selection.getRangeAt(0).getBoundingClientRect();
var bottom = rect.bottom + document.body.scrollTop;
var left = rect.left + document.body.scrollLeft
$("#popupContainer").css('top', bottom);
// Div exceeds left side
if (left - $("#popupContainer").outerWidth() / 2 < 0) {
$("#popupContainer").css('left', left);
// TODO: set the arrow to be left
}
// Div exceeds right side
else if (left + $("#popupContainer").outerWidth() / 2 > $(window).width()) {
$("#popupContainer").css('left', left + rect.width - $("#popupContainer").outerWidth());
// TODO: set the arrow to be right
}
else {
$("#popupContainer").css('left', left + rect.width / 2 - $("#popupContainer").outerWidth() / 2);
}
$("#popupContainer").show();
var ctx = $('#popupCanvas')[0].getContext("2d");
ctx.moveTo(10, 0);
ctx.lineTo(0, 10);
ctx.lineTo(20, 10);
ctx.fillStyle = "rgba(50,50,50,0.9)";
ctx.fill();
};
I'm trying to draw a popup using HTML5 canvas.
The popup is composed of a canvas that draws an arrow and a div below:
As you can clearly see, the color of the arrow is darker than the div's background-color.
The code is injected into every tab of the browser and the popup is shown upon selection of text.
Here's the drawing code:
var ctx = $('#popupCanvas')[0].getContext("2d");
ctx.moveTo(10, 0);
ctx.lineTo(0, 10);
ctx.lineTo(20, 10);
ctx.fillStyle = "rgba(50,50,50,0.9)";
ctx.fill();
and the CSS of the div is :
#popupMenu
{
line-height: 0px;
display:block;
background-color:rgba(50,50,50,0.9);
color:#ddd;
-webkit-box-shadow: 2px 2px 5px #555;
-webkit-border-radius:5px;
font-family: Century Gothic, sans-serif;
font-style: normal;
font-variant: normal;
font-weight: normal;
font-size:0px;
padding:5px;
width:345px;
}
As you can see, the colors are the same and yet they are rendered differently.
What am I doing wrong?
Why are there any differences? I'm using Webkit under chrome 15.
Edit1
I forgot to mention, the popup is running as injected javascript\css.
Adding the HTML source code:
<div id='popupContainer'>
<canvas id='popupCanvas' width='20' height='10'/>
<div id='popupMenu'>
</div>
</div>
and the full CSS:
#popupCanvas
{
position:relative;
margin:0px;
padding:0px;
}
#popupContainer
{
position:absolute;
z-index: 999999;
direction:ltr;
text-align:center;
}
#popupMenu
{
line-height: 0px;
display:block;
background-color:rgba(50,50,50,0.9);
color:#ddd;
-webkit-box-shadow: 2px 2px 5px #555;
-webkit-border-radius:5px;
font-family: Century Gothic, sans-serif;
font-style: normal;
font-variant: normal;
font-weight: normal;
font-size:0px;
padding:5px;
width:345px;
}
Edit2
Here's my entire code upon user selection :
this.onTextSelected = function (selText) {
selectedText = selText;
// check if we have value
var selection = window.getSelection();
var node = document.elementFromPoint(currentMousePos.x, currentMousePos.y);
if (node.value) {
rect = getTextBoundingRect(node, $(node).caret().start, $(node).caret().end);
}
else
rect = selection.getRangeAt(0).getBoundingClientRect();
var bottom = rect.bottom + document.body.scrollTop;
var left = rect.left + document.body.scrollLeft
$("#popupContainer").css('top', bottom);
// Div exceeds left side
if (left - $("#popupContainer").outerWidth() / 2 < 0) {
$("#popupContainer").css('left', left);
// TODO: set the arrow to be left
}
// Div exceeds right side
else if (left + $("#popupContainer").outerWidth() / 2 > $(window).width()) {
$("#popupContainer").css('left', left + rect.width - $("#popupContainer").outerWidth());
// TODO: set the arrow to be right
}
else {
$("#popupContainer").css('left', left + rect.width / 2 - $("#popupContainer").outerWidth() / 2);
}
$("#popupContainer").show();
var ctx = $('#popupCanvas')[0].getContext("2d");
ctx.moveTo(10, 0);
ctx.lineTo(0, 10);
ctx.lineTo(20, 10);
ctx.fillStyle = "rgba(50,50,50,0.9)";
ctx.fill();
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议尝试将其定义为 rgba 以外的其他内容作为潜在的解决方案,尽管这很奇怪。
另一方面,我会考虑使用伪元素(请参阅此处了解一篇很棒的文章)对于箭头来说,对于这样的细节使用整个画布元素似乎有点没有必要。虽然支持可能不是很好/一致,但对于画布来说也不是,所以我想说这可能是一个很好的交换。
I'd suggest trying to define it in something other than rgba as a potential fix, although it is quite strange.
On another point, i'd consider using a pseudo element (see here for a great article) for the arrow, it seems a bit unnecessary to use a whole canvas element for such a detail. Whilst support might not be fantastic/consistent, neither is that for canvas so I'd say it's likely a good swap.
我相信这是因为弹出背景穿过画布(alpha 0.9)。另一方面,弹出窗口本身是在浅色背景上绘制的。
尝试修改 alpha 值以查看这是否是问题的原因。
I'd believe this is because pop-up background shines through canvas (which has alpha 0.9). On the othe hand, pop-up itself is drawn on light background.
Try modify alpha values to see if this is the cause of the problem.
我无法重现这个问题。可以提供完整代码吗?
我在下面添加了一个带有稍大三角形的小提琴,并使用了非供应商前缀版本的边框半径和框阴影,以便它可以在 IE9、Opera 和 Mozilla 以及 webkit 浏览器上运行。
我还在底部 div 添加了 -5px 边距以导致重叠。
http://jsfiddle.net/thebeebs/tGzna/2/
三角形颜色看起来很好所有主要浏览器对我来说。我所能想到的是,您在图片中使用的CSS与您在问题中添加的CSS不同(这是可能的,因为您的图像中的div有黑色背景),页面上的代码嵌入会影响颜色,或者箭头下方有一些东西会影响 0.9 alpha。
我还在背景中添加了一些东西,看看这是否对 Canvas 元素和具有 Alpha 背景的 Div 产生不同的影响......但似乎没有。
I'm not able to reproduce this issue. Can you provide full code?
I added a fiddle below with a slighlty larger triangle and used un-vendor prefixed versions of border-radius and box shadow so that it works across IE9, Opera and Mozilla as well as webkit browser.
I also added a -5px margin to the bottom div to cause an overlap.
http://jsfiddle.net/thebeebs/tGzna/2/
The triangle colour looks fine across All major browsers to me. All I can think is that the CSS you are using in the picture differs from the CSS you have added in the question (which is possible since there is a black background to the div in your image), somthing on the page in which the code is embeded is effecting the color or there is something undernearth the arrow that is effecting the 0.9 alpha.
I also added something in the background to see if this effected Canvas elements and Div's with Alpha backgrounds differently... but it didn't seem to.