Jump to content

Help asked, html image draw

Go to solution Solved by Glennieboyyy007,

solved

This post has become irrelevant kind of. no need to reply on this post anymore!!!

 

 

Hey there,

 

So i am trying to make my own background that i will use with Wallpaper engine. i'm still pretty noob at programming, that's why i'm here.

 

I am trying to make a clock in html that uses images as hands of the clock. i have the clock itself working, thanks to the internet, but i seem to be unable to get the image as the hand of the clock.

This is what i have so far


<!DOCTYPE html>
<html>
<body>

<canvas id="canvas" width="1920" height="1080" style="background-color:#000000">
</canvas>

<img src="https://orig00.deviantart.net/cfd1/f/2018/199/c/0/mp7_by_ghbendr-dchktx7.png" alt="MP7" style="width:15%;">
<img src="https://orig00.deviantart.net/a223/f/2018/199/4/a/bearing_9_by_ghbendr-dchkug5.png" alt="Bearing_9" style="width:10%;">


<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var radius = canvas.height / 2;
ctx.translate(radius, radius);
radius = radius * 0.50
setInterval(drawClock, 1000/144);

function drawClock() {
  drawFace(ctx, radius);
  drawNumbers(ctx, radius);
  drawTime(ctx, radius); }

function drawFace(ctx, radius) {
  var grad;
  ctx.beginPath();
  ctx.arc(0, 0, radius, 0, 2*Math.PI);
  ctx.fillStyle = '#000000';
  ctx.fill();
  grad = ctx.createRadialGradient(0,0,radius*0.95, 0,0,radius*1.05);
  grad.addColorStop(0, '#C30000');
  grad.addColorStop(1, '#000000');
  ctx.strokeStyle = grad;
  ctx.lineWidth = radius*0.1;
  ctx.stroke();
  ctx.beginPath();
  ctx.arc(0, 0, radius*0.1, 0, 2*Math.PI);
  ctx.fillStyle = '#FFD800';
  ctx.fill(); }

function drawNumbers(ctx, radius) {
  var ang;
  var num;
  ctx.font = radius*0.15 + "px arial";
  ctx.textBaseline="middle";
  ctx.textAlign="center";
  for(num = 1; num < 13; num++){
    ang = num * Math.PI / 6;
    ctx.rotate(ang);
    ctx.translate(0, -radius*0.85);
    ctx.rotate(-ang);
    ctx.fillText(num.toString(), 0, 0);
    ctx.rotate(ang);
    ctx.translate(0, radius*0.85);
    ctx.rotate(-ang); } }

function drawTime(ctx, radius){
    var now = new Date();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    //hour
    hour=hour%12;
    hour=(hour*Math.PI/6)+
    (minute*Math.PI/(6*60))+
    (second*Math.PI/(360*60));
    drawHand(ctx, hour, radius*0.5, radius*0.07);
    //minute
    minute=(minute*Math.PI/30)+(second*Math.PI/(30*60));
    drawHand(ctx, minute, radius*0.8, radius*0.07);
    // second
    second=(second*Math.PI/30); }

function drawHand(ctx, pos, length, width) {
    ctx.beginPath();
    ctx.lineWidth = width;
    ctx.lineCap = "round";
    ctx.moveTo(0,0);
    ctx.rotate(pos);
    ctx.lineTo(0, -length);
    ctx.stroke();
    ctx.rotate(-pos); }

</script>

 

</body>
</html>

image.thumb.png.f3f35bc6d1dfea8a0e50e3ca63842db9.png

I have highlighted the part where i need to change things.

I know that i can't change the ctx and the word hour in the drawHand, but the radiuses i have too changes to a picture.

the pictures i am using are located below the clock for now.

 

If someone, after all this mess, wants to help me, thank you. If you dont want to, okay, np, all's good.

 

Thank you!

 

~i5-7600k @5GHz ~Be Quiet! Dark rock 3 ~MSI GTX 1070 Gaming X 8G ~Gigabyte GA-Z270-gaming K3 ~Corsair Vengeance Red led ~NZXT S340 Elite

Link to comment
https://linustechtips.com/topic/949217-help-asked-html-image-draw/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×