<html>
<head> </head>
<body>
<canvas
id="canvas"
style="display:block;margin:0 auto;border:1px solid #ccc"
>您的浏览器不支持canvas</canvas
>
<input
type="range"
id="scale-range"
min="0.5"
max="3.0"
step="0.01"
value="1.0"
style="display:block;margin:20px auto;width:800px;"
/>
<canvas
id="watermark-canvas"
style="display:none;margin:0 auto;border:1px solid #ccc"
>您的浏览器不支持canvas</canvas
>
</body>
</html>
<script>
window.onload = function () {
var canvas = document.getElementById('canvas')
canvas.width = 800
canvas.height = 800
var watermarkCanvas = null
var watermarkContext = null
var context = canvas.getContext('2d')
var slider = document.getElementById('scale-range')
var scale = slider.value
var img = new Image()
img.src = 'xxx.jpg'
img.onload = function () {
drawImageByScale(scale)
slider.onmousemove = function () {
scale = slider.value
drawImageByScale(scale)
}
}
}
function drawwater() {
watermarkCanvas = document.getElementById('watermark-canvas')
watermarkContext = watermarkCanvas.getContext('2d')
watermarkCanvas.width = 600
watermarkCanvas.height = 100
watermarkContext.font = 'bold 50px Arial'
watermarkContext.lineWidth = '1'
watermarkContext.fillStyle = 'rgba(255,255,255,0.5)'
watermarkContext.textBaseline = 'middle'
watermarkContext.fillText('==Jsopy==', 20, 50)
}
function drawImageByScale(scale) {
var imageWidth = 1152 * scale
var imageHeight = 768 * scale
var dx = canvas.width / 2 - imageWidth / 2
var dy = canvas.height / 2 - imageHeight / 2
context.clearRect(0, 0, canvas.width, canvas.height)
context.drawImage(img, dx, dy, imageWidth, imageHeight)
drawwater()
context.drawImage(
watermarkCanvas,
canvas.width - watermarkCanvas.width,
canvas.height - watermarkCanvas.height
)
}
</script>