简体   繁体   English

javascript,在函数中动态加载画布和绘图上下文; 上下文消失了

[英]javascript, dynamically loading canvases and drawing context in function; context disappears

To summarize, I am changing what canvases are shown via ajax calls and what gets drawn on each as well. 总而言之,我正在更改通过ajax调用显示的画布以及在每个画布上绘制的画布。 The main problem is that my main drawing function fails on getContext and there is other weird behavior like missing canvases (but they are definitely there). 主要问题是我的主绘图函数在getContext上失败,并且还有其他奇怪的行为,例如缺少画布(但它们肯定在那里)。

Here is the function I use to draw on my canvases: 这是我用来在画布上绘制的函数:

function PlotSet(plot_properties){
    var p = plot_properties;
    var canvas = document.getElementById(p.panel_name);
    var spinner_target = document.getElementById(p.spinner_name);
    if (!canvas) {
        alert('Error: Cannot find the canvas element!');
        return;
    } else if (!canvas.getContext) {
        alert('Error: Canvas context does not exist!');
        return;
    } else {
        var ctx = canvas.getContext('2d');
        /*Do the drawing*/
    }
}

I have 3 buttons in a section of my page that determine how many and the arrangement of the panels/canvas. 我在页面的一部分中有3个按钮,用于确定面板/画布的数量和排列方式。 When someone clicks one, another php page with that arrangement is loaded. 当某人单击时,将加载具有该排列的另一个php页面。

<div id="panel_single" name="panel_control" class="xs-btn sm-btn-h-grp sm-btn-one-panel"></div>
<div id="panel_split" name="panel_control" class="xs-btn sm-btn-h-grp sm-btn-four-panel"></div>
<div id="panel_rows" name="panel_control" class="xs-btn sm-btn-h-grp sm-btn-list-panel"></div>
<script>
    $("#panel_single_settings").hide();
    $("div[name=panel_control]").click(function(event) {
        var nav_id = $(this).attr("id");
        if(nav_id == "panel_single"){
            $("#panel_single_settings").show();
        }else{
            $("#panel_single_settings").hide();
        }
        //$("span[name=vnav-text]").removeClass("vnav-active");
        //$("#" + nav_id).addClass('vnav-active');
        refresh_content($(".widget-panel"), nav_id, function(){
            side_panel_hide($(".side_panel"), "600px");
        });
    });
</script>

Everything works when I click on these after loading page. 当我在加载页面后单击这些按钮时,一切正常。 But I have two issues when changing the drawing properties/redrawning. 但是更改图形属性/重新绘制时有两个问题。 (1) the PlotSet function fails on getContext for everything except the first canvas, when multiple canvases are shown, when this function is used to redraw an already drawn canvas (using a pop-up form with ajax to set what is redrawn). (1)PlotSet函数在getContext上对除第一个画布以外的所有对象都失败,当显示多个画布时,当此函数用于重绘已绘制的画布时(使用带有ajax的弹出式窗体设置要重绘的内容)。 (2) When only one panel is shown, the settings can be changed and the panel can be redrawn, but then when I then load a page with multiple panels, all panels but the first are blank (ie this page functions as expected, except when I redrawn a canvas on another page). (2)仅显示一个面板时,可以更改设置并可以重绘该面板,但是当我随后加载包含多个面板的页面时,除第一个面板外所有面板都是空白的(即该页面功能正常,除了当我在另一页上重新绘制画布时)。

Single Panel example: 单面板示例:

<script>
    var plot_properties = {
        //Settings to draw
    }
    PlotSet(plot_properties);
</script>

example of multi panel configuration: 多面板配置示例:

<canvas class="analytics-graph-medium" id="graph_send"></canvas>        
<canvas class="analytics-graph-medium" id="graph_recv"></canvas>
<canvas class="analytics-graph-medium" id="graph_cpu"></canvas>
<canvas class="analytics-graph-medium" id="graph_ram"></canvas>


<script>
    var split_send_plot_properties = {
        //Settings to draw
    }
    PlotSet(split_send_plot_properties);

    var split_recv_plot_properties = {
        //Settings to draw
    }
    PlotSet(split_recv_plot_properties);
    .
    .
    .
    //**************************************************************
    //Panel Settings Events
    //**************************************************************
    $("#cpu_panel_settings").on("click", function(){
        var panel_name = $(this).attr('id');
        set_side_panel_html($(".side_panel"), "/gui/pages/status/dialogs/panel_settings.php", split_cpu_plot_properties, function(){
            side_panel_show($(".side_panel"), "500px");
        });
    });
    .
    .
    .
</script>

And this is the submit button action for changing the settings: 这是用于更改设置的提交按钮操作:

$("#mod_panel").click(function(){
            plot_properties = {
                //lots of properties to be drawn
            }
            PlotSet(plot_properties);
            side_panel_hide($(".side_panel"), "600px");
        });

Well, I seem to have solved the problem. 好吧,我似乎已经解决了问题。 I still do not understand the problem, but the solution is to add a new canvas with the same name, remove the old canvas and them to re-grab the new canvas with the same old name with getElementById. 我仍然不明白问题所在,但是解决方案是添加一个具有相同名称的新画布,删除旧画布,然后使用getElementById重新获取具有相同旧名称的新画布。 But only do that when the canvas can be found, but the context does not exist... otherwise use the original context. 但是只有在可以找到画布但上下文不存在的情况下才这样做……否则请使用原始上下文。

function PlotSet(plot_properties){
        var p = plot_properties;
        var spinner_target = document.getElementById(p.spinner_name);
        var canvas = document.getElementById(p.panel_name);
        if (!canvas) {
            alert('Error: Cannot find the canvas element!');
            return;
        } else if (!canvas.getContext) {
            var new_canvas = document.createElement(p.panel_name);
            canvas.parentNode.insertBefore(new_canvas, canvas.nextSibling);
            canvas.parentNode.removeChild(canvas);
            canvas = document.getElementById(p.panel_name);
        } 
        var ctx = canvas.getContext('2d');
        //Draw Something
}

This makes everything work, all the dynamic page loading with multiple canvases on them, plus changing settings/drawing of any canvas, and switching pages after a chane is made. 这样可以使所有工作正常进行,所有动态页面均加载有多个画布,再更改任何画布的设置/绘制,并在进行色调处理后切换页面。

If anyone has any futher insight into what caused the problem or why this solution worked, I'm eager ears. 如果有人对导致问题的原因或此解决方案起作用的原因有进一步的了解,我会很想听到。

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

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