实现刮奖效果通常涉及到模拟刮刮卡的视觉效果,用户通过鼠标拖动来揭示隐藏的区域。以下是一个基本的实现步骤:
HTML
首先,创建一个基础的HTML结构:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scratch Card Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="scratch-card">
<canvas id="scratch-canvas"></canvas>
<div id="message" class="hidden">Congratulations! You Won!</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
添加一些样式,使刮奖效果更加真实:
/* styles.css */
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f4f4f4;
font-family: Arial, sans-serif;
}
#scratch-card {
position: relative;
width: 300px;
height: 200px;
border: 2px solid #ddd;
overflow: hidden;
border-radius: 10px;
}
#scratch-canvas {
width: 100%;
height: 100%;
background-color: #ccc;
}
#message {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
font-size: 24px;
text-align: center;
}
.hidden {
display: none;
}
JavaScript
编写JavaScript代码来实现刮奖逻辑:
// script.js
document.addEventListener('DOMContentLoaded', () => {
const canvas = document.getElementById('scratch-canvas');
const ctx = canvas.getContext('2d');
const message = document.getElementById('message');
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const scratchArea = 50; // Area to reveal on each mouse move
// Draw the scratch card background
ctx.fillStyle = '#888';
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
// Draw a grey rectangle over the canvas to simulate the scratchable area
ctx.fillStyle = '#ccc';
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
// Define the function to start scratching
let isScratching = false;
let startX, startY;
canvas.addEventListener('mousedown', (e) => {
isScratching = true;
startX = e.offsetX;
startY = e.offsetY;
});
canvas.addEventListener('mousemove', (e) => {
if (!isScratching) return;
const x = e.offsetX;
const y = e.offsetY;
ctx.globalCompositeOperation = 'destination-out';
ctx.beginPath();
ctx.arc(x, y, scratchArea, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
ctx.globalCompositeOperation = 'source-over';
checkWinCondition();
});
canvas.addEventListener('mouseup', () => {
isScratching = false;
});
canvas.addEventListener('mouseleave', () => {
isScratching = false;
});
const checkWinCondition = () => {
const imageData = ctx.getImageData(0, 0, canvasWidth, canvasHeight);
const data = imageData.data;
let revealedPixels = 0;
for (let i = 0; i < data.length; i += 4) {
if (data[i + 3] < 255) {
revealedPixels++;
}
}
const revealPercentage = (revealedPixels / (canvasWidth * canvasHeight)) * 100;
if (revealPercentage > 50) { // Adjust threshold as needed
message.classList.remove('hidden');
}
};
});
解释
- HTML部分:
- 包含一个
canvas
元素,用于绘制刮奖效果。 - 一个
div
显示中奖信息,初始设置为隐藏状态。
- 包含一个
- CSS部分:
- 设置页面居中显示。
- 为刮奖卡和
canvas
设置样式。 - 为中奖信息添加样式,使其居中显示。
- JavaScript部分:
- 在
DOMContentLoaded
事件中初始化canvas
上下文。 - 绘制灰色矩形作为刮奖区域。
- 监听鼠标事件,实现鼠标按下、移动和松开时的绘制逻辑。
- 在每次鼠标移动时,使用
globalCompositeOperation
设置为destination-out
,实现擦除效果。 checkWinCondition
函数计算已揭示的像素比例,当达到指定比例时显示中奖信息。
- 在
这个示例展示了一个基本的刮奖效果。你可以根据需要调整样式、逻辑和阈值,以实现不同的刮奖体验。