Jump to content

(HTML) replacing a line with an image

Go to solution Solved by Glennieboyyy007,

i solved it

note to admins, if you think this is a double post, as i asked something like this 21 hours ago, please delete that post!

 

Hey you, thanks for reading,

 

So i have come a small way with my html clock, but i cant seem to change the line to a picture.

It loads the picture in fine, but i cant get it to stop rotating the line and start rotating the image.

Also, im pretty terrible with html/coding, so i definitely don't have things looking nice and organized.

The part i'm struggling with is made bold so it's easier to find (it's all the way down).

 


<!DOCTYPE html>
<html>
<body>

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

 

<img id="MP7" src="https://orig00.deviantart.net/cfd1/f/2018/199/c/0/mp7_by_ghbendr-dchktx7.png" alt="MP7" width="0%">
<img id="Bearing_9" src="https://orig00.deviantart.net/7e3e/f/2018/200/d/a/bearing_9_by_ghbendr-dchkug5.png" alt="Bearing_9" width="0%">


<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.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));
    drawBearing_9(ctx, hour, 100, 5);
    //minute
    minute=(minute*Math.PI/30)+(second*Math.PI/(30*60));
    drawMP7(ctx, minute, 200, 5);
    // second
    second=(second*Math.PI/30); }

 

function drawBearing_9(ctx, pos, length, width) {
    ctx.beginPath();
    ctx.lineWidth = width;
    ctx.moveTo(0,0);
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    var img = document.getElementById("Bearing_9");
    ctx.drawImage(img,-10,-25,125, 81);
    ctx.rotate(pos);
    ctx.lineTo(0, -length);
    ctx.stroke();
    ctx.rotate(-pos); }

 

function drawMP7(ctx, pos, length, width) {
    ctx.beginPath();
    ctx.lineWidth = width;
    ctx.moveTo(0,0);
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    var img = document.getElementById("MP7");
    ctx.drawImage(img,-220,-35, 225, 113);
    ctx.rotate(pos);
    ctx.lineTo(0, -length);
    ctx.stroke();
    ctx.rotate(-pos); }

 

</script>

</body>

</html>

 

 

Tthe image below shows what the code makes now. What i'm trying to do is having the guns align with the lines, so i can delete the lines. Or if i'm able to have the guns stick to the line i can just make the line invisible, so that's also good.

 

image.thumb.png.a1b5bc065955729ac6c287c1f3b6e5eb.png

 

Thank you for trying to help!!

~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/949544-html-replacing-a-line-with-an-image/
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

×