有什么方法可以使“绘制图像”仅在按下键/单击时发生?
我对 javascript 很陌生,但我已经完成了 html 和 css。
有没有一种简单的方法可以让我完成这项工作?
<!doctype html>
<html>
<head>
<title>test</title>
<style type="text/css">
body {
background-color:#000000;
image-repeat:none;
margin: 0px;
cursor: none;
overflow: hidden;
}
</style>
</head>
<body>
<script type="text/javascript">
var imageWidthHalf, imageHeightHalf;
var canvas = document.createElement( 'canvas' );
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.style.display = 'block';
document.body.appendChild( canvas );
var context = canvas.getContext( '2d' );
var image = document.createElement( 'img' );
image.addEventListener('load', function() {
imageWidthHalf = Math.floor( this.width / 2 );
imageHeightHalf = Math.floor( this.height / 2 );
document.addEventListener( 'mousemove', onMouseEvent, false ); //mouseclick ?
} , false );
image.src ="cir.png"
//mouseclick ?
function onMouseEvent( event ) {
context.drawImage( image, event.clientX - imageWidthHalf , event.clientY - imageHeightHalf );
}
</script>
</body>