gpt4 book ai didi

javascript - 如何使用 Canvas 或 jQuery 将图像和文本放在背景图像上?

转载 作者:行者123 更新时间:2023-12-03 03:45:05 25 4
gpt4 key购买 nike

我正在尝试使用 Canvas/jQuery 将图像和文本放在背景图像上,但我不知道如何执行此操作。我需要像这张图片那样http://static.catfly.com/quiz/which-friend-should-be-kidnapted-by-aliens/en/cover.jpg

我已经尝试并找到了一些博客脚本,但它不是完整的脚本,它只是画了一个圆圈,有人可以帮助我吗?

下面是我的脚本。

<body onload="displayImage()">

<script type="text/javascript">

//Global variables
var myImage = new Image(); // Create a new blank image.

// Load the image and display it.
function displayImage()
{
// Get the canvas element.
canvas = document.getElementById("myCanvas");

// Make sure you got it.
if (canvas.getContext)
{
// Specify 2d canvas type.
ctx = canvas.getContext("2d");

// When the image is loaded, draw it.
myImage.onload = function() {

// Load the image into the context.
ctx.drawImage(myImage, 0, 0);

// Get and modify the image data.
changeImage();
}

// Define the source of the image.
myImage.src = "https://pbs.twimg.com/profile_image/843519123711803393/pyYe9LFq_400x400.jpg";
}
}

function changeImage()
{
ctx.strokeStyle = "white";
ctx.lineWidth = "100";
ctx.beginPath();
ctx.arc(100, 100, 150, 0, Math.PI * 2, true);
ctx.closePath();
ctx.stroke();
}
</script>

<canvas id="myCanvas" width="200" height="200"></canvas>

</body>

最佳答案

问题之一是 Canvas 只有 200x200,圆圈的线宽为 100,并且是白色,因此绘制时它会填充大部分 Canvas (在这种情况下,它也会稍微超出 Canvas 区域)。

而且,有问题的图像也有一个加载错误,这可能是这里的主要原因(应始终添加 onerror+onabort 处理程序)。

简单地调整线宽,固定图像,我还建议将 Canvas 设置得更大(可以使用图像大小来设置)。

关于javascript - 如何使用 Canvas 或 jQuery 将图像和文本放在背景图像上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45431031/

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