简体   繁体   English

列表内的HTML5 Canvas,无法在所有画布上绘制图像

[英]HTML5 Canvas inside list, cannot draw images on all

I am trying to draw images on HTML5 Canvas inside unordered list. 我正在尝试在无序列表内的HTML5 Canvas上绘制图像。 There is 1 canvas inside each li element and there are total of 20 li inside ul. 每个li元素内有1个画布,ul内部总共有20 li。 I am using below code to draw image on canvas, however only the last canvas gets image drawn on it. 我正在使用以下代码在画布上绘制图像,但是只有最后一个画布才能在其上绘制图像。 I am new to HTML5 not really sure what I am doing wrong, but I am doing something wrong for sure. 我是HTML5的新手,我不确定自己做错了什么,但是可以肯定我做错了什么。

<_ul id="ulDirectory" data-role="listview" data-filter="true">
<li data-role="fieldcontain" style="background: #dddddd; height: 80px">
            <div class="ui-grid-a">
                <div class="ui-block-a" style="width: 25%">
                    <canvas id="myCanvas<%: Html.DisplayFor(_m => item.EmployeeCode) %>" width="80" height="80" class="pull-left"></canvas>
                </div>
                <div class="ui-block-b" style="width: 75%; line-height: 25px">
                    <span><%: Html.DisplayFor(_m => item.FName) %>  <%: Html.DisplayFor(_m => item.LName) %></span><br />
                    <span><%: Html.DisplayFor(_m => item.Mobile) %> | <%: Html.DisplayFor(_m => item.EmployeeCode) %></span><br />
                    <span><%: Html.DisplayFor(_m => item.Email) %></span>
                </div>
            </div>
        </li>
</_ul>

<script type="text/javascript">
function displayImage() {`enter code here`
            $("ul#ulDirectory > li").find("canvas").each(function () {
                var myImage = new Image(); //**UPDATE**
                canvas = this;
                if (canvas.getContext) {
                    ctx = canvas.getContext("2d");
                    myImage.onload = function () {
                        ctx.drawImage(myImage, 0, 0, 80, 80);
                        ctx.strokeStyle = "white";
                        ctx.lineWidth = "25";
                        ctx.beginPath();
                        ctx.arc(40, 40, 52, 0, Math.PI * 2, true);
                        ctx.closePath();
                        ctx.strokeStyle = '#dddddd';
                        ctx.stroke();
                    }
                    myImage.src = "../../TempUpload/hitin831121.png";
                }
            });
$(document).ready(function () {
            displayImage();
        });
</script>

All your variables belong to a global scope and are getting overriden in each itteration. 您的所有变量都属于全局范围,并且在每次发出信号时都会被覆盖。 Use var keyword to declare the variables inside the each function scope. 使用var关键字在每个函数范围内声明变量。

$("ul#ulDirectory > li canvas").each(function () {
    var canvas = this;
    if (canvas.getContext) {
        var ctx = canvas.getContext("2d");
        var myImage = new Image();
        myImage.onload = function () {
            ctx.drawImage(myImage, 0, 0, 80, 80);
            ctx.strokeStyle = "white";
            ctx.lineWidth = "25";
            ctx.beginPath();
            ctx.arc(40, 40, 52, 0, Math.PI * 2, true);
            ctx.closePath();
            ctx.strokeStyle = '#dddddd';
            ctx.stroke();
        }
        myImage.src = ...

Here is a fiddle: http://jsfiddle.net/4Lozwkzq/1/ 这是一个小提琴: http : //jsfiddle.net/4Lozwkzq/1/

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

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