2012-06-14 6 views
2

JavaScript 프로젝트 (jquery 사용)가 있습니다. 그 안에는 코드로 캔버스를 그린 다음 이미지 배열을 삽입합니다. 그것은 오류없이 잘 나타냅니다.캔버스 html 태그

문제 :

var inter; 
var screen; 

function onClick(id){ 
    getScreen(id, function(data){ 
     screen=window.open('',id,'width=' + data.maxX + ',height=' + data.maxY , true); 
     if (screen.document.getElementById("screen") != null){  
      screen.document.getElementById("screen").innerHTML = ""; 
     } 
     screen.document.write('<div id="screen"><canvas id="screenCanvas" width=' + 
           data.maxX + ' height=' + data.maxY + 
           ' style="border:2px solid #000000;color:#000000;" ></canvas></div>'); 
     draw(data); 
     inter = setInterval(function(){screen(id); }, 5000); 
    }); 
} 

function screen(id){ 
    getScreen(id, function(data){ 
     if (screen.closed == true){ 
      clearInterval(inter); 
      return; 
     } 
     if((screen.document.getElementById('screenCanvas').width != data.maxX) || 
      (data.maxY != screen.document.getElementById('screenCanvas').height)){ 
      screen.close(); 
      screen=window.open('',id,'width=' + data.maxX + ',height=' + data.maxY , true);   
     screen=window.open('',id,'width=' + data.maxX + ',height=' + data.maxY , true); 
     if (screen.document.getElementById("screen") != null){  
      screen.document.getElementById("screen").innerHTML = ""; 
     } 
     screen.document.write('<div id="screen"><canvas id="screenCanvas" width=' + 
           data.maxX + ' height=' + data.maxY + 
           ' style="border:2px solid #000000;color:#000000;" ></canvas></div>'); 
     draw(data); 
    }); 
} 

function draw(data) { 
    var canvas = screen.document.getElementById('screenCanvas'); 
    var context = canvas.getContext('2d'); 
    var tileY = 0; 
    var tileX = 0; 
    var counter = 0; 
    var tileWidth = data.tileWidth; 
    var tileHeight = data.tileHeight; 
    for (var i=0;i<(data.maxX/data.tileWidth);i++){ 
     for (var j=0;j<(data.maxY/data.tileHeight);j++){ 
      var img = new Image(); 
      img.onload = (function(img, tileX, tileY, tileWidth, tileHeight){ 
       return function() { 
        context.drawImage(img,tileX, tileY, tileWidth, tileHeight); 
       } 
      })(img, tileX, tileY, tileWidth, tileHeight); 
      img.src = "http://myserver:8080/images/screen/tile/" + 
         data.tiles[counter].imageId; 
      tileX = tileX + parseInt(data.tileWidth); 
      counter ++; 
     } 
     tileY = tileY + parseInt(data.tileHeight); 
     tileX = 0; 
    } 
} 

</script> 

이 코드가 배열 (data) that contains list of (id, maxX, maxY, tileWidth, tileHeight, tiles[x, y, imageId]) 얻고,이 창에서 캔버스 코드를 작성 후, 새 창을 열고 다음 호출 이미지를 그릴 :later on, i try to get these images inside from canvas to compare if they have the same src or the same id to don't replace it with the new image

내 함수는 타이머가 5 초마다 작동합니다. 매 5 초 후 화면 메서드 호출을 통해 데이터를 다시 가져오고 화면이 열려 있는지 확인한 다음 내부 HTML을 모두 제거하여 새 캔버스를 다시 작성하고 크기가 변경되는지 테스트합니다.

이 코드는 아무런 오류없이 잘 동작하지만 캔버스 내부의 이미지를 가져 와서 동일한 이미지 ID를 가지고 있는지 테스트해야합니다. 다시 다운로드하지 마십시오. (이미지 ID를 저장하지 않습니다. img.id에 이미지를 저장하거나 imageId가 고유하게 만드는 이미지 경로의 일부이므로 src로 이미지를 가져올 수 있습니다.

참고 :

id[unique](is the id of the user), 
maxX(maximum width of the screen), 
maxY(maximum height of the screen), 
tileWidth(width of each image), 
tileHeight(height of each image), 
tiles(list of images) 
    x(left position of the image) 
    y(top postion of the image) 
    imageId[unique](id of each image) 
example: data[id:1,maxX:1920,maxY:1080,tileWidth:480,tileHeight:270,tiles:(x:2,y:1,imageId:1a)] 

어떤 도움?

+1

내가 아는 한 캔버스 객체에 그려진 이미지를 얻을 수 없다는 것을 안다. 당신이 할 수있는 일은 이미지의 ID를 포함하는 배열을 만들고 새로 다운로드 한 이미지의 ID와 비교하는 것이다. –

+0

만약 내가 16 이미지, 그리고 당신이 언급 한 ID를 비교, 그 10 ID가 다르다는 것을 발견, 어떻게 캔버스의 참조에 의해 그 10 이미지를 대체 할 수 있습니다 –

+0

캔버스 대신 HTML에서 이미지를 나타내는 다른 방법은 무엇입니까? –

답변

1

으로 Ravi Jain은 캔버스 객체에 그려진 이미지를 얻을 수 없다고 말했고 robertc은 img 요소를 사용할 수 있다고 말했습니다.

는 IMG 요소의 사용하지 캔버스를 지정 이러한 기능을 시도해보십시오

function onClick(id){ 
    getScreen(id, function(data){ 
     var inter; 
     var screen; 
     var width = parseInt(data.maxX) + parseInt(data.tileWidth); 
     var height = parseInt(data.maxY) + parseInt(data.tileHeight); 
     screen=window.open('',id,'width=' + width + ',height=' + height , true); 
     draw(screen, data); 
     inter = setInterval(function(){screen(screen, inter, id); }, 5000); 
    }); 
} 

function screen(screen, inter, id){ 
    getScreen(id, function(data){ 
     if (screen.closed == true){ 
      clearInterval(inter); 
      return; 
     } 
     if((screen.document.getElementById("screen").style.width != data.maxX + "px") || 
      (screen.document.getElementById("screen").style.height != data.maxY + "px")){ 
      screen.close(); 
      var width = parseInt(data.maxX) + parseInt(data.tileWidth); 
      var height = parseInt(data.maxY) + parseInt(data.tileHeight); 
      screen=window.open('',id,'width=' + width + ',height=' + height , true); 
     } 
     draw(screen, data); 
    }); 
} 

function draw(screen, data) { 
    var screenDiv = screen.document.getElementById("screen"); 
    if (screenDiv == null) screen.document.write('<div id="screen" style="width:' + 
       data.maxX + '; height:' + data.maxY + '; " style="margin: 0 auto;" >'); 
    var tileY = 0; 
    var tileX = 0; 
    var counter = 0; 
    for (var i=0;i<(data.maxX/data.tileWidth);i++){ 
     for (var j=0;j<(data.maxY/data.tileHeight);j++){ 
      var imageSource = screen.document.getElementById("id_" + counter); 
      var path = "http://myserver:8080/images/screen/tile/" + 
         data.tiles[counter].imageId; 
      if (imageSource == null){ 
       screen.document.write('<img id="id_' + counter + '" src="' + path + 
       '" height="' + data.tileHeight + '" width="' + data.tileWidth + '" />'); 
      }else if(imageSource.src != path){ 
       imageSource.src = path; 
      } 
      tileX = tileX + parseInt(data.tileWidth); 
      counter ++; 
     } 
     tileY = tileY + parseInt(data.tileHeight); 
     tileX = 0; 
    } 
    if (screenDiv == null) screen.document.write('</div>'); 
} 

</script> 

코드 잘 작동되는 사실이지만, 하나의 예를 들어, 당신은 상단에 var에 화면 var에 간 선언 함수에없는 스크립트는 첫 번째 사용자를 클릭하여 화면을 가져 오는 경우 (즉, 코드에서 내가 무엇을 이해했는지와 같이) 첫 번째 사용자의 타이머가 중지되고 두 번째 사용자가 시작된다는 것을 의미합니다. 이 코드는이 문제를 해결하고 이미지 소스가 바뀌면 문제를 해결합니다.

희망이 당신을 도와주세요,

GOOD LUCK;

+0

감사합니다 대니 사실이야, 나도이 문제가있어, 고마워 남자 –

0

canvas에 그림이 그려지면 픽셀입니다. 픽셀을 만드는 데 사용 된 객체는 유지되지 않습니다. 이런 종류의 정보를 원할 경우 canvas 외부에서 직접 관리해야하거나 like SVG의 객체를 보유하는 무언가를 사용해야합니다.