简体   繁体   中英

Dynamically generated canvas elements in ASP.NET

I have multiple canvas elements which appear dynamically on my page based on VB code.

This is what my aspx page looks like:

<canvas id="canvasnhlrc" width="225" height="200" runat="server"></canvas>
<canvas id="canvascwl" width="225" height="200" runat="server"></canvas>
<canvas id="canvashslanguages" width="225" height="200" runat="server"></canvas>

This is what my aspx.vb code looks like:

canvasnhlrc.Visible = False
canvascwl.Visible = False
canvashslanguages.Visible = False

        If RouteData.Values("mediasubType") IsNot Nothing Then

            Dim qsubMedia As String = RouteData.Values("mediasubType")

            Select Case qsubMedia

                Case "nhlrc"

                    canvasnhlrc.Visible = True

                Case "cwl"

                    canvascwl.Visible = True

                Case "hslanguages"

                    canvashslanguages.Visible = True

            End Select

        End If

So, if my page is example.com/nhlrc , then the canvas is generated as

<canvas id="ContentPlaceHolder2_canvasnhlrc" width="225" height="200"></canvas>

Accordingly, I have formatted my js file as follows:

function init() 
{ 
    var w1 = document.getElementById('ContentPlaceHolder2_canvasnhlrc')
    var w1x = w1.getContext('2d');
    w1x.shadowOffsetX = 0;
    w1x.shadowOffsetY = 0;
    w1x.shadowBlur = 20;
    w1x.shadowColor = "rgba(0, 0, 0, 0.75)";
    w1x.fillStyle = 'rgb(218, 233, 246)';
    w1x.beginPath();
    w1x.moveTo(25, 25);
    w1x.lineTo(175, 25);
    w1x.lineTo(175, 50);
    w1x.lineTo(200, 75);
    w1x.lineTo(175, 100);
    w1x.lineTo(175, 175);
    w1x.lineTo(25, 175);
    w1x.closePath();
    w1x.fill();

    var w2 = document.getElementById('ContentPlaceHolder2_canvascwl')
    var w2x = w2.getContext('2d');
    w2x.shadowOffsetX = 0;
    w2x.shadowOffsetY = 0;
    w2x.shadowBlur = 20;
    w2x.shadowColor = "rgba(0, 0, 0, 0.75)";
    w2x.fillStyle = 'rgb(12, 64, 114)';
    w2x.beginPath();
    w2x.moveTo(25, 25);
    w2x.lineTo(175, 25);
    w2x.lineTo(175, 50);
    w2x.lineTo(200, 75);
    w2x.lineTo(175, 100);
    w2x.lineTo(175, 175);
    w2x.lineTo(25, 175);
    w2x.closePath();
    w2x.fill();

    var w3 = document.getElementById('ContentPlaceHolder2_canvashslanguages')
    var w3x = w3.getContext('2d');
    w3x.shadowOffsetX = 0;
    w3x.shadowOffsetY = 0;
    w3x.shadowBlur = 20;
    w3x.shadowColor = "rgba(0, 0, 0, 0.75)";
    w3x.fillStyle = 'rgb(12, 64, 114)';
    w3x.beginPath();
    w3x.moveTo(25, 25);
    w3x.lineTo(175, 25);
    w3x.lineTo(175, 50);
    w3x.lineTo(200, 75);
    w3x.lineTo(175, 100);
    w3x.lineTo(175, 175);
    w3x.lineTo(25, 175);
    w3x.closePath();
    w3x.fill();
}
onload = init;

Sadly, I can only get the first canvas element (NHLRC) to appear while the subsequent canvases ( example.com/cwl , etc.) do not render. There is nothing wrong with the code because when I comment out the other two elements in the JS code, it works fine, so what am I missing, doing wrong, or what's conflicting that's not allowing each of the canvas elements to render in its corresponding page?

Your init function has too many responsibilities (find, verify and draw) * 3 . When any one of those responsibilities meets with a failure, ie can't find an element, the show stops. In your case w1, w2, w3 will only be defined in their respective pages, not for every page.

You can simplify it as below:

function draw(theCanvasEl, theFill) 
{ 
    var w1x = theCanvasEl.getContext('2d');
    w1x.shadowOffsetX = 0;
    w1x.shadowOffsetY = 0;
    w1x.shadowBlur = 20;
    w1x.shadowColor = "rgba(0, 0, 0, 0.75)";
    w1x.fillStyle = theFill;
    w1x.beginPath();
    w1x.moveTo(25, 25);
    w1x.lineTo(175, 25);
    w1x.lineTo(175, 50);
    w1x.lineTo(200, 75);
    w1x.lineTo(175, 100);
    w1x.lineTo(175, 175);
    w1x.lineTo(25, 175);
    w1x.closePath();
    w1x.fill();
}

function init()
{
  var canvases = [['ContentPlaceHolder2_canvasnhlrc', 'rgb(218, 233, 246)'],
     ['ContentPlaceHolder2_canvascwl', 'rgb(12, 64, 114)'],
     ['ContentPlaceHolder2_canvashslanguages', 'rgb(12, 64, 114)']];

   for(var i=0;i<canvases.length;i++)
   { 
       var theCanvas = document.getElementById(canvases[i][0]);
       if (theCanvas!=null) {
         draw(theCanvas, canvases[i][1]);
         break;              
       }
   }
}

onload = init;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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