简体   繁体   English

画布HTML5,IE,Chrome和FireFox,字体和偏移属性

[英]Canvas HTML5, IE vs Chrome vs FireFox - font and offset properties

I developed an HTML5 canvas graph that retrieves SQL-stored information and plots them graphically (color-coded) on a HTML5 canvas. 我开发了一个HTML5画布图,该图检索SQL存储的信息,并在HTML5画布上以图形方式(用颜色编码)绘制它们。 The canvas allows scrolling over a timeline to show various events that have occured (Say between 1990 - 2013). 画布允许在时间轴上滚动以显示已发生的各种事件(在1990年至2013年之间说)。

IE works like a charm. IE的魅力十足。

Chrome has issues with the context font being muddy/bleeding effect - i was using monospace 11px, I changed it to verdana later on, but still a bit muddy with chrome. Chrome的上下文字体存在混浊/渗色效果时出现问题-我使用的是11px等宽字体,后来我将其更改为verdana,但是铬仍然有点混浊。 Firefox doesn't have this issue. Firefox没有这个问题。

Firefox has an issue, where it retrives and plots information on the canvas, but when i click on the canvas to scroll in the past or future of current position on the timeline, the entire canvas dissapears. Firefox有一个问题,它在画布上检索和绘制信息,但是当我单击画布在时间轴上的当前位置的过去或将来滚动时,整个画布就会消失。 Chrome doesn't have this issue. Chrome没有这个问题。

I 've tried to explain my issues on this question, if you need more clarification please ask. 我已尝试解释有关此问题的问题,如果您需要更多说明,请询问。

here is the sample code:- 这是示例代码:-

http://jsfiddle.net/WNpKE/16/ http://jsfiddle.net/WNpKE/16/

(if you click on the link and open it in IE, FireFox, and Chrome, I hope that the issue will become more evident.) (如果您单击链接并在IE,FireFox和Chrome中打开它,我希望问题会变得更加明显。)

  // defining the canvas element
            var can = document.getElementById("myCanvas"),
                ctx = can.getContext("2d"),
                dragging = false,
                translated = 0,
                lastX = 0,
                grid = (function (dX, dY) {
                    var can = document.createElement("canvas"),
                        ctx = can.getContext('2d');
                    can.width = dX;
                    can.height = dY;
                    // fill canvas color
                    ctx.fillStyle = 'black';
                    ctx.fillRect(0, 0, dX, dY);

                    // x axis
                    ctx.strokeStyle = 'orange';
                    ctx.moveTo(.5, 0.5);
                    ctx.lineTo(dX + .5, 0.5);
                    ctx.stroke();

                    // y axis
                    ctx.moveTo(.5, .5);
                    ctx.lineTo(.5, dY + .5);
                    ctx.stroke();

                    return ctx.createPattern(can, 'repeat');
                })(72, 50);

            ctx.save();
            /*ctx.scale(1, -1);
            ctx.translate(0, -900);*/



            // when mouse is clicked on canvas
            can.onmousedown = function (e) {
                var evt = e || event;
                dragging = true;
                lastX = evt.offsetX;
            }


            // when mouse is clicked again and the canvas is deselected  
            window.onmouseup = function () {
                dragging = false;
            }




            window.onmousemove = function (e) {
                var evt = e || event;
                if (dragging) {
                    var delta = evt.offsetX - lastX;
                    translated += delta;
                    //console.log(translated);
                    ctx.restore();
                    ctx.clearRect(0, 0, 930, 900);
                    ctx.save();
                    ctx.translate(translated, 0);
                    lastX = evt.offsetX;
                    timeline();
                }
            }






            // Function that draws the timeline on the xy grid along with data points.
            function timeline() {

                // fill canvas color
                ctx.fillStyle = "black";
                ctx.fillRect(-translated, 0, 930, 900);
                ctx.fillStyle = grid;
                ctx.fillRect(-translated, -250, 930, 900);


                // y-co-ordinate texts - Home, Office, Emergency, etc...
                ctx.strokeStyle = "White";
                ctx.font = "10px Verdana";
                ctx.strokeText("Home", -translated, 510);

                ctx.strokeStyle = "White";
                ctx.font = "10px Verdana";
                ctx.strokeText("Office", -translated, 460);

                ctx.strokeStyle = "White";
                ctx.font = "10px Verdana";
                ctx.strokeText("Emergency", -translated, 410);

                ctx.strokeStyle = "White";
                ctx.font = "10px Verdana";
                ctx.strokeText("Foster Home", -translated, 360);

                ctx.strokeStyle = "White";
                ctx.font = "10px Verdana";
                ctx.strokeText("SNF", -translated, 310);

                ctx.strokeStyle = "White";
                ctx.font = "10px Verdana";
                ctx.strokeText("LTC", -translated, 260);

                ctx.strokeStyle = "White";
                ctx.font = "10px Verdana";
                ctx.strokeText("Drug/Rehab", -translated, 210);

                ctx.strokeStyle = "White";
                ctx.font = "10px Verdana";
                ctx.strokeText("Hospital", -translated, 160);

                ctx.strokeStyle = "White";
                ctx.font = "10px Verdana";
                ctx.strokeText("Hospice", -translated, 110);


                ctx.strokeStyle = "White";
                ctx.font = "10px Verdana";
                ctx.strokeText("ANP Exams", -translated, 540);
                ctx.strokeStyle = "White";
                ctx.font = "10px Verdana";
                ctx.strokeText("Life Event", -translated, 560);
                ctx.strokeStyle = "White";
                ctx.font = "10px Verdana";
                ctx.strokeText("Care Plan", -translated, 610);

I have changed a bit since this code, but the basic idea of click and scroll is still the same. 自此代码以来,我进行了一些更改,但是单击和滚动的基本思想仍然相同。 Thanks. 谢谢。

Use fillText instead of strokeText . 使用fillText代替strokeText

The FF error is happening because the FF event object doesn't have an offsetX property. 发生FF错误是因为FF事件对象没有offsetX属性。 Use pageX instead. 请改用pageX

Updated fiddle: http://jsfiddle.net/WNpKE/18/ 更新的小提琴: http : //jsfiddle.net/WNpKE/18/

Take a look at the highcharts API. 看看highcharts API。 It's free and has an enormous amount of features. 它是免费的,并具有大量功能。 I've recently used it in a web application effort querying data from an SQL database not unlike what you're doing. 我最近在Web应用程序中使用它来查询SQL数据库中的数据,这与您正在做的工作没什么不同。 It works across all the major browsers. 它适用于所有主要浏览器。

I am guessing that the canvas element (being a new feature of html5) isn't being rendered the same way between browsers. 我猜想canvas元素(是html5的新功能)在浏览器之间的渲染方式不一样。 You may be better off either re-writing in javascript / java or just using the highcharts framework as-is. 您可能最好用javascript / java进行重写,或者仅使用highcharts框架。 I realise this isn't a solution to your current problem, but it may save you some time. 我知道这不是解决您当前问题的方法,但是可以为您节省一些时间。

Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM