图片 1 是用画布创建的图片。图2是我下载的图片。我不知道为什么图片的形状会变回矩形。我希望它可以和我在网页上看到的一样形状(图1)。感谢您的任何帮助!!!!
这是我用来在画布上绘制图片并将画布转换为图像的代码。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<a id="final" href="#" class="btn btn-success btn_lg">Hello<img src = "contacts.png" alt= "your image" width="30" height="30" /> </a>
<img id="scream" width="220" height="277"
src="contacts.png" alt="contacts">
<p>Canvas:</p>
<canvas id="myCanvas" width="240" height="297"
style="border-radius: 15px;">
Your browser does not support the HTML5 canvas tag.
</canvas>
<style>
</style>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText("Hello World",70,260);
ctx.fillStyle="#0000ff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
window.onload = function() {
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var img = document.getElementById("scream");
ctx.drawImage(img, 10, 10);
};
function download() {
var canvas = document.getElementById('myCanvas');
dataUrl = canvas.toDataURL();
imageFoo = document.createElement('img');
imageFoo.src = dataUrl;
// Style your image here
imageFoo.style.width = '100px';
imageFoo.style.height = '100px';
imageFoo.style.borderRadius= "12px";
document.body.appendChild(imageFoo);