Solved

HTML - How to draw a 'target'  / concentric circles on a canvas

Posted on 2014-07-20
2
540 Views
Last Modified: 2014-07-20
How can use  JS  to draw a 'target'  (Like the Target icon ) or concentric circles when a  canvas is click on a an specific point?

 var ctx = canvas.getContext('2d');
????
0
Comment
Question by:JElster
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40207638
maybe something like this? http://jsfiddle.net/robert_schutt/8usRk/
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext('2d');

var C_RADIUS = 20, C_NUM = 3, C_WIDTH = 10;

$('#canvas').click(function(e){
    var offset = $(this).offset();
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    var x = e.pageX - offset.left - C_WIDTH / 2;
    var y = e.pageY - offset.top - C_WIDTH / 2;
    ctx.lineWidth = C_WIDTH;
    ctx.strokeStyle = 'black';
    ctx.beginPath();
    ctx.moveTo(x - C_NUM * C_RADIUS, y);
    ctx.lineTo(x + C_NUM * C_RADIUS, y);
    ctx.closePath();
    ctx.stroke();
    ctx.beginPath();
    ctx.moveTo(x, y - C_NUM * C_RADIUS);
    ctx.lineTo(x, y + C_NUM * C_RADIUS);
    ctx.closePath();
    ctx.stroke();
    ctx.strokeStyle = 'red';
    for (var c = 0; c <= C_NUM; c++) {
        ctx.beginPath();
        ctx.arc(x, y, C_WIDTH / 2 + c * C_RADIUS, 0, 2 * Math.PI, true);
        ctx.closePath();
        ctx.stroke();
    }
});

Open in new window

0
 
LVL 1

Author Comment

by:JElster
ID: 40207923
thx again!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Easy responsive table out of existing table 28 55
How can I increase the size of this logo 5 34
How to make a moving character 3 53
Change Background Color of Website 5 15
This article discusses four methods for overlaying images in a container on a web page
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
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…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now