之前的课程中,如何使用ChatGPT制作大字报式小红书首图
发现有的同学遇到了GPT生成的渐变色背景代码,渐变色背景无法存为图片的问题,我这里做了一个小白也能懂的简单的解释、解决方案和判断方法,
原理
我们通过GPT生成渐变色背景的代码,如果是下面这种代码样式:
style="background-color: #0093E9; background-image: linear-gradient(160deg, #0093E9 0%, #80D0C7 100%);
那么浏览器就不能将这个背景顺利的存为图片,原因就在于:
这个渐变色背景是通过CSS样式应用于<body>
或<head>
元素的background-color
和background-image
属性。这样的渐变色背景无法直接保存为图像,因为浏览器不会提供将CSS样式直接转换为图像的功能。
这个与我们在浏览器右键存图时存为png还是jpg无关的,因为浏览器都没法将这个CSS样式存下来。
唯一能存下来的方式,是使用Canvas绘图,Canvas是基于像素的绘图技术,所以这种方式设置的渐变色背景,是可以作为图片元素存下来的。
Canvas绘图的代码大体如下样式:
<body>
<canvas id="coverCanvas"></canvas>
const gradient = ctx.createLinearGradient(0, 0, width, height);
gradient.addColorStop(0, '#0093E9');
gradient.addColorStop(1, '#80D0C7');
</body>
判断依据
所以判断的依据,其实就是看涉及到颜色的那一串代码,前面是style样式,还是单独的存在于<body>
和</body>
之间。
解决方案
那么如果GPT给了我们第一种的代码,该如何解决呢?其实很简单,继续调教它,调教话术如下:
你提供的代码将渐变背景设置为了css样式,我需要将这个渐变背景仅用于canvas绘制的画布上,请重新生成完整的代码
附案例:
两种代码样式附案例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>封面图设计</title>
</head>
<body>
<body style="background-color: #0093E9; background-image: linear-gradient(160deg, #0093E9 0%, #80D0C7 100%); margin: 0; padding: 0;">
<canvas id="cover" width="1080" height="1440"></canvas><script>
// 获取canvas元素
var canvas = document.getElementById("cover");
var ctx = canvas.getContext("2d");
// 设置小标题样式
ctx.font = "bold 60px Arial";
ctx.textAlign = "center";
ctx.fillStyle = "gold";
ctx.fillText("111", canvas.width/2, canvas.height*0.18);
// 设置描述样式
ctx.font = "30px Arial";
ctx.fillStyle = "gold";
ctx.fillText("222", canvas.width/2, canvas.height*0.22);
// 设置大标题样式
ctx.font = "bold 120px Arial";
ctx.fillStyle = "gold";
ctx.fillText("111", canvas.width/2, canvas.height*0.4);
// 设置副标题样式
ctx.font = "70px Arial";
ctx.fillStyle = "white";
ctx.fillText("222", canvas.width/2, canvas.height*0.5);
// 设置页脚样式
ctx.font = "40px Arial";
ctx.fillStyle = "gold";
ctx.fillText("333", canvas.width/2, canvas.height*0.95);
</script>
</body> </html>
<!DOCTYPE html>
<html>
<head>
<title>封面图设计</title>
<style>
body {
margin: 0;
padding: 0;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="coverCanvas"></canvas>
<script>
// 获取画布和上下文
const canvas = document.getElementById('coverCanvas');
const ctx = canvas.getContext('2d');
// 设置画布尺寸
const width = 1080;
const height = 1440;
canvas.width = width;
canvas.height = height;
// 创建渐变背景
const gradient = ctx.createLinearGradient(0, 0, width, height);
gradient.addColorStop(0, '#0093E9');
gradient.addColorStop(1, '#80D0C7');
// 填充渐变背景
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, width, height);
// 设置文字样式
ctx.font = '60px Arial';
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
// 绘制小标题
const smallTitle = '产品设计AI时代必备';
const smallTitleY = height * 0.18;
ctx.fillText(smallTitle, width / 2, smallTitleY);
// 绘制描述
ctx.font = '30px Arial';
const description = 'Must read by PM';
const descriptionY = height * 0.22;
ctx.fillText(description, width / 2, descriptionY);
// 绘制大标题
ctx.font = 'bold 120px Arial';
const bigTitle = '即时设计的AI功能';
const bigTitleY = height * 0.4;
ctx.fillText(bigTitle, width / 2, bigTitleY);
// 绘制副标题
ctx.font = '70px Arial';
const subTitle = 'AI快速生成高质量产品原型';
const subTitleY = height * 0.5;
ctx.fillText(subTitle, width / 2, subTitleY);
// 绘制页脚
ctx.font = '40px Arial';
const footer = 'Dawson';
const footerY = height - height * 0.05;
ctx.fillText(footer, width / 2, footerY);
</script>
</body>
</html>
GPT使用地址
AI提示词课程合集
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容