页面没有看到选择框,双击画布也没有响应
时间: 2024-04-14 16:28:53 浏览: 103
非常抱歉,我之前的回答可能有误导。根据你的需求,我已经更新了代码。请查看以下整合后的代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>MathJax Example</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
<style>
#canvas {
position: relative;
width: 100%;
height: 95vh;
background-color: #CCFFCC;
}
.mathjax-formula {
font-size: 2.8em;
position: absolute;
cursor: move;
padding: 5px;
border-radius: 5px;
color: black;
}
.mathjax-formula:focus {
outline: none;
border: 2px solid black;
}
#selection-box {
position: absolute;
bottom: 10px;
left: 10px;
}
</style>
</head>
<body>
<div id="canvas">
<div id="mathjax-formulas-container"></div>
</div>
<div id="selection-box">
<input type="radio" name="options" value="text" checked> Text
<input type="radio" name="options" value="image"> Image
</div>
<script>
var formulas = [];
var selectedFormula = null;
var isDragging = false;
function createFormula(latexCode, x, y) {
var mathjaxFormulasContainer = document.getElementById("mathjax-formulas-container");
var newFormula = document.createElement("div");
newFormula.className = "mathjax-formula";
newFormula.style.left = x + "px";
newFormula.style.top = y + "px";
newFormula.contentEditable = true;
newFormula.innerText = latexCode;
mathjaxFormulasContainer.appendChild(newFormula);
formulas.push({
latexCode: latexCode,
position: {
x: x,
y: y
}
});
MathJax.Hub.Queue(["Typeset", MathJax.Hub, newFormula]);
newFormula.addEventListener("dblclick", function(event) {
event.stopPropagation();
});
newFormula.addEventListener("mousedown", function(event) {
event.stopPropagation();
selectedFormula = newFormula;
isDragging = true;
newFormula.style.cursor = "move";
});
newFormula.addEventListener("mouseup", function(event) {
event.stopPropagation();
isDragging = false;
newFormula.style.cursor = "auto";
});
newFormula.addEventListener("mousemove", function(event) {
event.stopPropagation();
if (isDragging && selectedFormula === newFormula) {
var newX = event.clientX - selectedFormula.offsetWidth / 2;
var newY = event.clientY - selectedFormula.offsetHeight / 2;
selectedFormula.style.left = newX + "px";
selectedFormula.style.top = newY + "px";
}
});
newFormula.addEventListener("keydown", function(event) {
if (event.key === "Enter") {
event.preventDefault();
newFormula.blur();
MathJax.Hub.Queue(["Typeset", MathJax.Hub, newFormula]);
}
});
}
document.getElementById("canvas").addEventListener("dblclick", function(event) {
var selectedOption = getSelectedOption();
if (selectedOption === "text") {
navigator.clipboard.readText().then(function(text) {
if (text) {
var latexCode = text.trim();
if (latexCode) {
createFormula(latexCode, event.clientX, event.clientY);
}
}
});
} else if (selectedOption === "image") {
navigator.clipboard.read().then(function(data) {
if (data && data.length > 0) {
var imageBlob = data[0];
var imageUrl = URL.createObjectURL(imageBlob);
var mathjaxFormulasContainer = document.getElementById("mathjax-formulas-container");
var newImage = document.createElement("img");
newImage.src = imageUrl;
newImage.className = "mathjax-formula";
newImage.style.left = event.clientX + "px";
newImage.style.top = event.clientY + "px";
mathjaxFormulasContainer.appendChild(newImage);
formulas.push({
latexCode: "",
position: {
x: event.clientX,
y: event.clientY
}
});
}
});
}
});
document.getElementById("canvas").addEventListener("click", function(event) {
if (event.target === document.getElementById("canvas")) {
if (selectedFormula) {
selectedFormula.style.border = "none";
selectedFormula = null;
isDragging = false;
}
}
});
function getSelectedOption() {
var options = document.getElementsByName("options");
for (var i = 0; i < options.length; i++) {
if (options[i].checked) {
return options[i].value;
}
}
return null;
}
</script>
</body>
</html>
```
这次更新的代码中,我添加了一个新的`<div>`元素,用于放置选择框。选择框包含两个选项:Text(文本)和Image(图片)。你可以选择其中一个选项,然后在双击画布时执行对应的操作。
请注意,选择框的HTML代码被放置在画布之外,这样它才不会干扰画布内部的交互。选择框的样式通过CSS进行了定义,使其位于画布的左下角。
希望这次更新的代码能够满足你的需求,如果还有其他问题,请随时告诉我。
阅读全文