有关拖动图像并将其放入画布的分步指南
P粉207483087
P粉207483087 2024-02-17 14:46:45
[HTML讨论组]

我正在尝试将一些图像拖放到画布内,其尺寸与我制作的尺寸相同,但它不起作用。

//Javascript per la creazione di cirucuiti elettrici

const NUMERO_CICLO = 990;
const NUMERO_CICLO_INTERNO = 60;


const resistor = document.getElementById("component_circuit_resistor");
const condensator = document.getElementById("component_circuit_condensator");
const tranistor = document.getElementById("component_circuit_tranistor");
const alimentator = document.getElementById("component_circuit_alimentator");
const circuit = document.getElementById("components_circuit");
const back_button = document.getElementById("back-button");
const clear_button = document.getElementById("clear-button");
const draggable = document.querySelectorAll(".draggable");
const container = document.querySelectorAll(".container");
const canvas = document.getElementById("canvas");
const foward_button = document.getElementById("foward-button");

canvas.width = 1500;
canvas.height = 855;
canvas.style.backgroundColor = "lightgrey";
circuit.appendChild(canvas);
canvas.style.borderRadius = "10px";
canvas.style.marginLeft = "auto";
canvas.style.marginRight = "auto";
canvas.style.display = "block";
const ctx = canvas.getContext("2d");


const circles = [];
const lines = [];
const lines_c = [];
var deletedLines = [];

for (let y = 20; y <= NUMERO_CICLO; y += 20) {
  for (let i = 0; i < NUMERO_CICLO_INTERNO; i++) {
    circles.push({
      x: 13 + i * 25,
      y,
      radius: 5,
      color: "grey",
    });
  }
}

let startCircle = null;
let endCircle = null;


function draw() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  circles.forEach((circle) => {
    ctx.beginPath();
    ctx.arc(circle.x, circle.y, circle.radius, 0, Math.PI * 2);
    ctx.fillStyle = circle.color;
    ctx.fill();
  });
  lines.forEach((line) => {
    ctx.beginPath();
    ctx.lineWidth = 4;
    ctx.moveTo(line.start.x, line.start.y);
    ctx.lineTo(line.end.x, line.end.y);
    ctx.stroke();
  });
  if (startCircle && endCircle) {
    ctx.beginPath();
    ctx.lineWidth = 4;
    ctx.moveTo(startCircle.x, startCircle.y);
    ctx.lineTo(endCircle.x, endCircle.y);
    ctx.stroke();
  }
  requestAnimationFrame(draw);
}
draw();

function goBack() {
  if (lines.length > 0 && back_clicked == true) {
    const eliminato = lines.pop();
    deletedLines.push(eliminato);
  }
  else if(foward_clicked == true && deletedLines.length > 0){
    const lastDeleted = deletedLines[deletedLines.length - 1];
    if (!lines.includes(lastDeleted)) {
      lines.push(lastDeleted);
      deletedLines.pop();
    }
  }
  //se non ci sono linee da eliminare mando alert
  else if (lines.length == 0 && back_clicked == true) {
    alert("There are no lines to delete");
  }
}

function clearCanvas() {
    if(confirm("Are you sure you want to delete the circuit?")){
      lines.length = 0;
      deletedLines.length = 0;
      lastDeleted = null;
    }
}

function getNearestCircle(x, y) {
  let nearestCircle = null;
  let nearestDistance = Infinity;
  circles.forEach((circle) => {
    const distance = Math.sqrt((circle.x - x) ** 2 + (circle.y - y) ** 2);
    if (distance < nearestDistance && distance < 30) {
      nearestCircle = circle;
      nearestDistance = distance;
    }
  });
  return nearestCircle;
}

let isDrawing = false;

canvas.addEventListener("mousedown", (event) => {
  const x = event.offsetX;
  const y = event.offsetY;
  const circle = getNearestCircle(x, y);
  if (circle) {
    startCircle = circle;
    endCircle = { x, y };
    isDrawing = true;
  }
});

canvas.addEventListener("mousemove", (event) => {
  if (isDrawing) {
    endCircle.x = event.offsetX;
    endCircle.y = event.offsetY;
  } else {
    const x = event.offsetX;
    const y = event.offsetY;
    const circle = getNearestCircle(x, y);
    if (circle) {
      circles.forEach((circle) => {
        circle.color = "grey";
      });
      circle.color = "red";
    } else {
      circles.forEach((circle) => {
        circle.color = "grey";
      });
    }
  }
});

canvas.addEventListener("mouseup", () => {
    if (isDrawing) {
        const x = endCircle.x;
        const y = endCircle.y;
        const circle = getNearestCircle(x, y);
        if (circle) {
        lines.push({
            start: startCircle,
            end: circle,
        });
        }
        isDrawing = false;
        startCircle = null;
        endCircle = null;
    }
});

//back_button.addEventListener("click", goBack);
//foward_button.addEventListener("click", goBack);
clear_button.addEventListener("click", clearCanvas);
var back_clicked = false;
back_button.addEventListener("click", function(){
  back_clicked = true;
  goBack();
  back_clicked = false;
})

var foward_clicked = false;
foward_button.addEventListener("click", function () {
  foward_clicked = true;
  goBack();
  foward_clicked = false;
})

  //risposta stack overflow
const draggableImages = document.querySelectorAll('img[draggable]');
for (const img of draggableImages)
  img.ondragstart = (ev) => {
    ev.dataTransfer.setData('text', img.src);
  };

canvas.ondragover = (ev) => ev.preventDefault(); // IMPORTANT

canvas.ondrop = (ev) => {
  const img = new Image();
  img.src = ev.dataTransfer.getData('text');
  img.onload = () => {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.drawImage(img, 0, 0);
  };
};  
@import url('https://fonts.googleapis.com/css2?family=Poppins&display=swap');
html,body{
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
    background-color: #ede7e1;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    font-size: 14px;
    color: #333;
    line-height: 1.5;
    overflow-x: hidden;
}

img{
    width: 100%;
    height: 100%;
}

#h1_disegna{
    text-align: center;
}
#h1_breadboard{
    text-align: center;
    position: relative;
    top: -550px;
}

#h1_titolo{
    font-size: 60px;
    text-align: center;
}
#h3_componenti_circuit{
    border-style: solid;
    width: 100px;
    border-color: black rigid 1px;
    list-style-type: none;
    margin-left: 0px;
    padding-left: 0px;
    background-color: sandybrown;
    position: relative;
    top: 85px;
}

#h3_componenti_br{
    border-style: solid;
    width: 100px;
    border-color: black rigid 1px;
    list-style-type: none;
    margin-left: 0px;
    padding-left: 0px;
    background-color: sandybrown;
    position: relative;
    top: -500px;
}

#h4_footer{
    font-size: 26px;
}


.elementi_disegno{
    position: relative;
    top: -520px;
}

#canvas{
    position: relative;
    top: -520px;
    
}

#back-button{
    position: relative;
    top: 62%;
    left: 42.5%;
    transform: translate(-50%, -50%);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    font-size: 24px;
    background-color: rgba(244, 165, 96, 0.473);
    border-radius: 6px;
    cursor: pointer;
}

#foward-button{
    position: relative;
    top: 62%;
    left: 43%;
    transform: translate(-50%, -50%);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    font-size: 24px;
    background-color: rgba(244, 165, 96, 0.473);
    border-radius: 6px;
    cursor: pointer;
}

#clear-button{
    border-radius: 6px;
    position: relative;
    top: 62%;
    left: 44.5%;
    transform: translate(-50%, -50%);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    font-size: 24px;
    background-color: rgba(244, 165, 96, 0.473);
    cursor: pointer;
}
#components_circuit_border{
    border-style: solid;
    width: 100px;
    border-color: black rigid 1px;
    list-style-type: none;
    margin-left: 0px;
    padding-left: 0px;
    background-color: antiquewhite;
    cursor: move;
    position: relative;
    top: 85px;
}

#components_br_border{
    border-style: solid;
    width: 100px;
    border-color: black rigid 1px;
    list-style-type: none;
    margin-left: 0px;
    padding-left: 0px;
    background-color: antiquewhite;
    cursor: move;
    position: relative;
    top: -500px;
}

#components_circuit{
    list-style-type: none;
    margin-left: 0px;
    padding-left: 0px;
}
.footer{
    text-align: center;
    background-color: darkgray;
    left: 0;
    margin-bottom: 0;
    height: 40px;
    width: 100%;
}

.material-symbols-outlined{
    display: inline-flex;
    vertical-align: -3px;
}



    
    
    
    
    
    From Circuit to Breadboard


    

From Circuit to Breadboard

Componenti:







Disegna il tuo circuito!



Breadboard

Componenti:







我正在尝试将一些图像拖放到画布中。它允许我拖动它,但当我想放下它时它不起作用。代码如下:

Componenti:

这是一个 HTML 示例,您可以看到我想将图像放入画布中。这是画布的 JavaScript:

const draggableClass = document.getElementsByClassName("draggable");
const draggable = document.querySelectorAll(".draggable");
const container = document.querySelectorAll(".container");

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
canvas.width = 1500;
canvas.height = 855;
canvas.style.backgroundColor = "lightgrey";
canvas.style.borderRadius = "10px";
canvas.style.marginLeft = "auto";
canvas.style.marginRight = "auto";
canvas.style.display = "block";

P粉207483087
P粉207483087

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号