user8700773
user8700773

Reputation:

linking a website to an image

So I am using this to display images on a site

var canvas = document.getElementById('myCanvas'),
context = canvas.getContext('2d');

images();
function images() {
image1 = new Image();

image1.src = 'https://lh3.googleusercontent.com/7PP1DeqGfPdu-12ULf1baVkQ7B-
lZvbjef7uPg5ZWOs5OX_4psOdKJO8RpgaXKhN9OlC=w200-h300';

image1.onload = function () {
    context.drawImage(image1, 10, 10, 220, 300);
}

But now I've decided I want to link a site to each of those images when they are clicked.

Initially, I thought I could just use the normal href way, but that cant be done in this case. Is there another way I can set this up?

Upvotes: 0

Views: 44

Answers (2)

Faan Veldhuijsen
Faan Veldhuijsen

Reputation: 106

To expand on the previous answer you can create click events by adding event listeners.

var canvas = document.getElementById('myCanvas');

canvas.addEventListener("click", function (event) {
    // User clicked on canvas
    window.location = 'https://google.com/'
});

Upvotes: 0

Amor
Amor

Reputation: 75

catch the click event and redirect to the page/site you want by window.location='https://yourwebpage.com'

Upvotes: 1

Related Questions