gpt4 book ai didi

javascript - Canvas - 当我将 放入 div 时不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 07:01:11 24 4
gpt4 key购买 nike

我想要画一些 Canvas 标签,当我放入 div 时它不起作用。我从 github 中获取了这段代码。它在 div 中不起作用。github 网址 => https://github.com/GeekLaunch/simple-drawing-web-app

我该怎么做?其实我不知道 Canvas 库。但是这些代码对我来说已经足够了。在我的元素中,我使用 bootstrap css 库。


var canvas, ctx,
brush = {
x: 0,
y: 0,
color: '#000000',
size: 10,
down: false,
},
strokes = [],
currentStroke = null;

function redraw() {
ctx.clearRect(0, 0, canvas.width(), canvas.height());
ctx.lineCap = 'round';
for (var i = 0; i < strokes.length; i++) {
var s = strokes[i];
ctx.strokeStyle = s.color;
ctx.lineWidth = s.size;
ctx.beginPath();
ctx.moveTo(s.points[0].x, s.points[0].y);
for (var j = 0; j < s.points.length; j++) {
var p = s.points[j];
ctx.lineTo(p.x, p.y);
}
ctx.stroke();
}
}

function init() {
canvas = $('#draw');
ctx = canvas[0].getContext('2d');

function mouseEvent(e) {
brush.x = e.pageX;
brush.y = e.pageY;

currentStroke.points.push({
x: brush.x,
y: brush.y,
});

redraw();
}

canvas.mousedown(function (e) {
brush.down = true;

currentStroke = {
color: brush.color,
size: brush.size,
points: [],
};

strokes.push(currentStroke);

mouseEvent(e);
}).mouseup(function (e) {
brush.down = false;

mouseEvent(e);

currentStroke = null;
}).mousemove(function (e) {
if (brush.down)
mouseEvent(e);
});

$('#save-btn').click(function () {
window.open(canvas[0].toDataURL());
});

$('#undo-btn').click(function () {
strokes.pop();
redraw();
});

$('#clear-btn').click(function () {
strokes = [];
redraw();
});

$('#color-picker').on('input', function () {
brush.color = this.value;
});

$('#brush-size').on('input', function () {
brush.size = this.value;
});
}

$(init);
body {
margin: 0;
}

.top-bar {
display: flex;
flex-direction: row;
background-color: #3af;
border-bottom: 2px solid black;
position: absolute;
width: 100%;
}

.top-bar * {
margin: 5px 10px;
}

#draw {
display: block;
}
:<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="top-bar">
<button id="save-btn">Save</button>
<button id="undo-btn">Undo</button>
<button id="clear-btn">Clear</button>
<input type="color" id="color-picker"></input>
<input type="range" id="brush-size" min="1" nax="50" value="10"></input>
</div>
<div style="border:1px solid red;width:500px;height:500px;margin:auto;">
<canvas id="draw" style="border:1px solid red;"></canvas>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="draw.js"></script>
</body>
</html>

最佳答案

根据我的观察,保存按钮在 Chrome 上不起作用。因为 Google Chrome 显然已经删除了对顶部框架导航的支持,您可以在此处查看更多信息:https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/GbVcuwg_QjM

您可以尝试将 Canvas 数据渲染到 iFrame。

将以下函数添加到您的 draw.js 文件中:

function debugBase64(base64URL){
var win = window.open();
win.document.write('<iframe src="' + base64URL + '" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>');
}

现在在点击事件发生时运行函数。

$('#save-btn').click(function () {
debugBase64(canvas[0].toDataURL());
});

希望它有用!

关于javascript - Canvas - 当我将 <canvas> 放入 div 时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52022820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com