?
Solved

HTML5 - Canvas, draw a grid - horizontal and vertical lines over an image.

Posted on 2014-10-07
2
Medium Priority
?
719 Views
Last Modified: 2014-10-08
Hi..
Any idea how I can draw a grid with horizontal and vertical lines over an existing image in a canvas?
grid-over-image.jpg
0
Comment
Question by:JElster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 40367891
based on code from one of your previous questions try something like this:
<html>
<body>
<div id="images"></div>
<canvas  style="margin:0;padding:0;position:relative;left:150px;top:50px;" id="imgCanvas" width="500" height="500"></canvas>
<script>
var canvas = document.getElementById("imgCanvas");
var context = canvas.getContext("2d");

var img = new Image();
img.src = 'http://c3e308.medialib.glogster.com/media/6f/6f51bc3a6754a352d1b831d02fb1be7676317170f47ea6ffab4587de281ba6c7/patrick-star-hammer-jpg.jpg';
img.onload = function() {
    context.drawImage(this, 0, 0);
    drawGrid();
};

var scalex = 2, scaley = 2;
context.scale(scalex, scaley);   // Zoom 200 %

function drawGrid() {
    var x,
        y,
        xx = canvas.width / scalex,
        yy = canvas.height / scaley,
        nx = 9, // number of rows
        ny = 4; // number of columns
    context.beginPath();
    for (x = 1; x < nx; x++) {
        y = xx * x / nx;
        context.moveTo(0, y);
        context.lineTo(xx, y);
    }
    context.closePath();
    context.lineWidth = 5 / scalex;
    context.strokeStyle = "#d3d3d3";
    context.stroke();
    context.beginPath();
    for (y = 1; y < ny; y++) {
        x = yy * y / ny;
        context.moveTo(x, 0);
        context.lineTo(x, yy);
    }
    context.closePath();
    context.lineWidth = 5 / scaley;
    context.strokeStyle = "#d3d3d3";
    context.stroke();
}

function draw(e) {
    var pos = getMousePos(canvas, e);
    posx = pos.x;
    posy = pos.y;
    context.fillStyle = "#0000ff";
    context.fillRect(posx / scalex, posy / scaley, 4, 4);
}
window.addEventListener('mousemove', draw, false);

function getMousePos(canvas, evt) {
    var rect = canvas.getBoundingClientRect();
    return {
      x: evt.clientX - rect.left,
      y: evt.clientY - rect.top
    };
}
</script>

Open in new window

0
 
LVL 1

Author Closing Comment

by:JElster
ID: 40369558
Awesome again!
thx
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question