<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>居中放大按钮示例</title>
<style>
button {
width: 300px;
height: 100px;
font-size: 40px;
border-radius: 100px;
border-top: 1px solid rgba(255, 255, 255, 0.9);
border-left: 1px solid rgba(255, 253, 253, 0.776);
border-right: 2px solid rgba(255, 253, 253, 0.7);
border-bottom: 2px solid rgba(255, 253, 253, 0.7);
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #808080;
box-shadow: 8px 0px 40px rgba(0, 0, 0, 0.8);
font-weight: bold;
text-shadow: 0px 1px 1px #ffffff, 0px 1px 1px #000000ba;
color: rgba(14, 14, 14, 0.915);
}
</style>
</head>
<body>
<button id="btn">Button</button>
</body>
<script>
// 获取按钮和视频元素
const btn = document.getElementById('btn');
// 获取用户媒体
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => {
// 播放摄像头视频流
const video = document.createElement('video');
video.srcObject = stream;
video.play();
// 定义更新函数
function update() {
// 获取视频帧
const track = stream.getVideoTracks()[0];
const imageCapture = new ImageCapture(track);
imageCapture.grabFrame()
.then(imageBitmap => {
// 将图像绘制到 canvas 上
const canvas = document.createElement('canvas');
canvas.width = imageBitmap.width;
canvas.height = imageBitmap.height;
const ctx = canvas.getContext('2d');
// 将图像水平翻转,实现镜像效果
ctx.scale(-1, 1);
ctx.translate(-canvas.width, 0);
ctx.filter = 'blur(3px)';
// 绘制图像中心位置
const centerX =100;
const centerY = 500;
// 只绘制中间部分
const cropWidth = 400;
const cropHeight = 600;
const cropLeft = centerX - cropWidth / 4;
const cropTop = centerY - cropHeight / 4;
ctx.drawImage(
imageBitmap,
cropLeft,
cropTop,
cropWidth,
cropHeight,
50,
-6,
canvas.width,
canvas.height
);
// 将 canvas 转换为 data URL,并将其设置为按钮的背景
const dataURL = canvas.toDataURL('image/jpeg');
btn.style.backgroundImage = `url(${dataURL})`;
// 在下一帧更新
requestAnimationFrame(update);
})
.catch(error => {
console.error('获取帧失败', error);
});
}
// 在第一帧更新
requestAnimationFrame(update);
})
.catch(error => {
console.error('获取摄像头失败', error);
});
</script>
</html>
2023年8月24日更新
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>按钮不锈钢LLOG.cc</title>
<style>
button {
width: 300px;
height: 100px;
font-size: 40px;
border-radius: 100px;
border-top: 1px solid rgba(255, 255, 255, 0.9);
border-left: 1px solid rgba(255, 253, 253, 0.776);
border-right: 2px solid rgba(255, 253, 253, 0.7);
border-bottom: 2px solid rgba(255, 253, 253, 0.7);
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #808080;
box-shadow: 8px 0px 40px rgba(0, 0, 0, 0.8);
font-weight: bold;
text-shadow: 0px 1px 1px #ffffff, 0px 1px 1px #000000ba;
color: rgba(14, 14, 14, 0.915);
}
.controls {
position: absolute;
top: 20px;
right: 20px;
display: flex;
flex-direction: column;
align-items: flex-end;
}
</style>
</head>
<body>
<button id="btn">Button</button>
<div class="controls">
<label for="brightness">亮度</label>
<input type="range" id="brightness" min="0.5" max="2" step="0.1" value="1">
<label for="contrast">对比度</label>
<input type="range" id="contrast" min="0.5" max="2" step="0.1" value="1">
<label for="opacity">不锈钢透明度</label>
<input type="range" id="opacity" min="0" max="1" step="0.1" value="0.3">
</div>
</body>
<script>
// 获取当前页面的缩放级别
function getZoomLevel() {
return Math.round(window.devicePixelRatio * 100);
}
// 设置页面缩放级别
function setZoomLevel(zoomPercentage) {
const scale = zoomPercentage / 100;
document.body.style.zoom = scale;
}
// 默认将页面缩放调整到170%
setZoomLevel(170);
// 可以在需要的时候调用 setZoomLevel 方法来调整缩放级别
// 获取按钮和视频元素
const btn = document.getElementById('btn');
const brightnessInput = document.getElementById('brightness');
const contrastInput = document.getElementById('contrast');
const opacityInput = document.getElementById('opacity');
// 获取用户媒体
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => {
// 播放摄像头视频流
const video = document.createElement('video');
video.srcObject = stream;
video.play();
// 定义更新函数
function update() {
// 获取视频帧
const track = stream.getVideoTracks()[0];
const imageCapture = new ImageCapture(track);
imageCapture.grabFrame()
.then(imageBitmap => {
// 将图像绘制到 canvas 上
const canvas = document.createElement('canvas');
canvas.width = imageBitmap.width;
canvas.height = imageBitmap.height;
const ctx = canvas.getContext('2d');
// 将图像水平翻转,实现镜像效果
ctx.scale(-1, 1);
ctx.translate(-canvas.width, 0);
// 调整图像亮度和对比度
ctx.filter = `brightness(${brightnessInput.value}) contrast(${contrastInput.value})`;
// 绘制图像中心位置
const centerX = 0;
const centerY = 400;
// 只绘制中间部分
const cropWidth = 700;
const cropHeight = 700;
const cropLeft = centerX - cropWidth / 4;
const cropTop = centerY - cropHeight / 4;
ctx.drawImage(
imageBitmap,
cropLeft,
cropTop,
cropWidth,
cropHeight,
50,
-6,
canvas.width,
canvas.height
);
// 添加模糊效果
ctx.filter = 'blur(2px)';
// 设置不锈钢颜色和透明度
ctx.fillStyle = `rgba(255, 255, 255, ${opacityInput.value})`;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 绘制完整效果后,将 canvas 转换为 data URL,并将其设置为按钮的背景
const dataURL = canvas.toDataURL('image/jpeg');
btn.style.backgroundImage = `url(${dataURL})`;
// 在下一帧更新
requestAnimationFrame(update);
})
.catch(error => {
requestAnimationFrame(update);
console.error('获取帧失败,无视错误重新执行!', error);
});
}
// 在第一帧更新
requestAnimationFrame(update);
})
.catch(error => {
console.error('获取摄像头失败', error);
});
</script>
</html>