1

Maybe you can advice me, I need to draw with canvas using the coordinates of images as vertex, but I have the images into a table(html table) so I don't know how to do that, how to do the draw inside the table or over the table. For example you have an image on coordinates(60,90) another on (79,45) and other on (70,64), i need to make a triangle with that coordinates, but the images are into a table(each image in different cells. This is my draw code:

function draw(elem) { //elem is the image

var image= elem;
var position = image.getBoundingClientRect();
//alert(posicion.top+" "+ posicion.right+" "+posicion.bottom+" "+posicion.left);

var canvas = document.getElementById('myCanvas');
var contexto = canvas.getContext('2d');
       contexto.beginPath();
       contexto.moveTo(position.top,50);
       contexto.lineTo(position.top,150);
       contexto.lineTo(position.top+100,150);
       contexto.fill();
}

and this is my canvas:

<canvas id="myCanvas" width="700" height="700">Your browse don't support canvas</canvas>

I put it under the table code and I call the function in other function. If you could help me it would be great. Thanks!

5
  • 2
    sorry , your question is not clear, could you please add relevant details? Commented May 3, 2015 at 16:52
  • I'll chaged it, it's better now? Commented May 3, 2015 at 21:20
  • 1
    still confusing. what table are you referring to? A database table, an HTML table, or a wooden table? Commented May 3, 2015 at 21:24
  • 1
    oh, just put the html table inside an absolutely positioned div and put the div over the canvas element using CSS. Calculate your images offset after placing the table on desired spot. Commented May 3, 2015 at 21:27
  • yes it's a html table (I just change it), okay i'm going to try it Commented May 3, 2015 at 21:38

2 Answers 2

2

I hope I understand your question correctly: you have images in an actual Table element (<table></table>) and images in some cells. You have a canvas placed below it which you want to draw lines on to connect the images in the table.

To do this just subtract the canvas element's absolute position from the image's absolute position. This will make the image's position relative to the canvas.

For example

var canvas = document.getElementById('myCanvas');
var canvasPos = canvas.getBoundingClientRect();
var position = image.getBoundingClientRect();

var x = position.left - canvasPos.left;
var y = position.top - canvasPos.top;

var contexto = canvas.getContext('2d');
contexto.beginPath();
contexto.moveTo(x, y);
... next image
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah it works too, that it's what i was trying to do, thanks!
1

jsBin demo

Let's say you have a table and canvas inside a parent like:

<div id="wrapper">
  <canvas id="lines"></canvas>
  <table id="table">
      <!-- 7*7 -->
  </table>
</div>

#wrapper{
  position:relative;
  margin:0 auto;
  width: 700px;
  height:700px;
}
#wrapper canvas{
  display:block;
  position:absolute;
}
#wrapper table{
  position: absolute;
}
#wrapper table td{
  background: rgba(0,0,0,0.1);
  width: 100px;
  height: 100px;
  vertical-align: middle;
}
#wrapper table td img{
  display: block;
  opacity:0.4;
}

You need to connect your images using lines,
you're probably interested for the image center but you need also to account the parent offset, so you need to subtract that position from the brc (getBoundingClientRect) of each image and it's td parentNode height/width:

var table = document.getElementById("table");
var images = table.getElementsByTagName("img");
var canvas = document.getElementById("lines");
var ctx = canvas.getContext("2d");
var x, y; // Remember coordinates

canvas.width  = table.offsetWidth;
canvas.height = table.offsetHeight;


function connect( image ) {
  var im = new Image();
  im.onload = function(){ // make sure image is loaded
    var tabBcr = table.getBoundingClientRect();
    var imgBcr = image.getBoundingClientRect();
    ctx.beginPath();
    ctx.moveTo(x, y);
    x = imgBcr.left + (image.parentNode.offsetWidth / 2) - tabBcr.left;
    y = imgBcr.top + (image.parentNode.offsetHeight / 2) - tabBcr.top;
    ctx.lineTo(x, y);
    ctx.stroke();
  };
  im.src = images[i].src;
}


for(var i=0; i<images.length; i++){
  connect( images[i] );
}

4 Comments

I'm confusing, from where did you get the x,y of ctx.moveTo()? it's the same as you declared before? var x,y; //Remember coordinates?
@AliATriple exactly. When instantiated, var x and y are null, so preventing from drawing a line at 0,0. On every other iteration of the loop those variables change to the respective values of the image center position.
A last question, if a want to close the form, i just have to move the coordinates to the first image, isn't it?
@AliATriple my code is not perfect cause it starts a bit at random depending on the loaded images order... had no time to improve it also due to a lack of info from your side about how it all actually kooks (HTML, CSS etc). Would me much easier if you explained a bit better what you have currently. Any way, you've found your answer! Happy coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.