gpt4 book ai didi

javascript - ASP.NET 中动态生成的 Canvas 元素

转载 作者:行者123 更新时间:2023-11-28 06:40:00 27 4
gpt4 key购买 nike

我有多个 Canvas 元素,它们基于 VB 代码动态显示在我的页面上。

这就是我的 aspx 页面的样子:

<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>

这就是我的 aspx.vb 代码的样子:

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

所以,如果我的页面是 example.com/nhlrc ,则 Canvas 生成为

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

因此,我将 js 文件格式化如下:

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;

遗憾的是,我只能让第一个 Canvas 元素(NHLRC)出现,而后续 Canvas ( example.com/cwl 等)不会渲染。代码没有任何问题,因为当我注释掉 JS 代码中的其他两个元素时,它工作正常,所以我错过了什么,做错了什么,或者有什么冲突不允许每个 Canvas 元素在其相应的中渲染页面?

最佳答案

你的 init 函数有太多职责(查找、验证和绘制)* 3 。当其中任何一项职责失败时,即找不到元素,表演就会停止。在您的情况下 w1, w2, w3 将仅在各自的页面中定义,而不是每个页面。

您可以将其简化如下:

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;

关于javascript - ASP.NET 中动态生成的 Canvas 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33925174/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com