以上代码用于使用缩放。它仅用于缩放图像。
现在如何将以下代码合并到html5-canvas中 html文件包含java脚本和canvas.I需要缩放画布
我需要将缩放代码合并到下面的html代码中,以便我可以缩放cavas
<html>
<canvas src = "zoom.css"></canvas>
<script type="text/javascript">
var canvas = document.getElementsByTagName('canvas')[0];
canvas.width = 800;
canvas.height = 600;
var gkhead = new Image;
window.onload = function(){
var ctx = canvas.getContext('2d');
trackTransforms(ctx);
function redraw(){
// Clear the entire canvas
var p1 = ctx.transformedPoint(0,0);
var p2 = ctx.transformedPoint(canvas.width,canvas.height);
ctx.clearRect(p1.x,p1.y,p2.x-p1.x,p2.y-p1.y);
ctx.save();
ctx.setTransform(1,0,0,1,0,0);
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.restore();
ctx.drawImage(gkhead,0,0);
}
redraw();
var lastX=canvas.width/2, lastY=canvas.height/2;
var dragStart,dragged;
canvas.addEventListener('mousedown',function(evt){
document.body.style.mozUserSelect = document.body.style.webkitUserSelect = document.body.style.userSelect = 'none';
lastX = evt.offsetX || (evt.pageX - canvas.offsetLeft);
lastY = evt.offsetY || (evt.pageY - canvas.offsetTop);
dragStart = ctx.transformedPoint(lastX,lastY);
dragged = false;
},false);
canvas.addEventListener('mousemove',function(evt){
lastX = evt.offsetX || (evt.pageX - canvas.offsetLeft);
lastY = evt.offsetY || (evt.pageY - canvas.offsetTop);
dragged = true;
if (dragStart){
var pt = ctx.transformedPoint(lastX,lastY);
ctx.translate(pt.x-dragStart.x,pt.y-dragStart.y);
redraw();
}
},false);
canvas.addEventListener('mouseup',function(evt){
dragStart = null;
if (!dragged) zoom(evt.shiftKey ? -1 : 1 );
},false);
var scaleFactor = 1.1;
var zoom = function(clicks){
var pt = ctx.transformedPoint(lastX,lastY);
ctx.translate(pt.x,pt.y);
var factor = Math.pow(scaleFactor,clicks);
ctx.scale(factor,factor);
ctx.translate(-pt.x,-pt.y);
redraw();
}
var handleScroll = function(evt){
var delta = evt.wheelDelta ? evt.wheelDelta/40 : evt.detail ? -evt.detail : 0;
if (delta) zoom(delta);
return evt.preventDefault() && false;
};
canvas.addEventListener('DOMMouseScroll',handleScroll,false);
canvas.addEventListener('mousewheel',handleScroll,false);
};
gkhead.src = 'file:///home/karthikksk/Desktop/practice%20/charlie.jpg';
// Adds ctx.getTransform() - returns an SVGMatrix
// Adds ctx.transformedPoint(x,y) - returns an SVGPoint
function trackTransforms(ctx){
var svg = document.createElementNS("http://www.w3.org/2000/svg",'svg');
var xform = svg.createSVGMatrix();
ctx.getTransform = function(){ return xform; };
var savedTransforms = [];
var save = ctx.save;
ctx.save = function(){
savedTransforms.push(xform.translate(0,0));
return save.call(ctx);
};
var restore = ctx.restore;
ctx.restore = function(){
xform = savedTransforms.pop();
return restore.call(ctx);
};
var scale = ctx.scale;
ctx.scale = function(sx,sy){
xform = xform.scaleNonUniform(sx,sy);
return scale.call(ctx,sx,sy);
};
var rotate = ctx.rotate;
ctx.rotate = function(radians){
xform = xform.rotate(radians*180/Math.PI);
return rotate.call(ctx,radians);
};
var translate = ctx.translate;
ctx.translate = function(dx,dy){
xform = xform.translate(dx,dy);
return translate.call(ctx,dx,dy);
};
var transform = ctx.transform;
ctx.transform = function(a,b,c,d,e,f){
var m2 = svg.createSVGMatrix();
m2.a=a; m2.b=b; m2.c=c; m2.d=d; m2.e=e; m2.f=f;
xform = xform.multiply(m2);
return transform.call(ctx,a,b,c,d,e,f);
};
var setTransform = ctx.setTransform;
ctx.setTransform = function(a,b,c,d,e,f){
xform.a = a;
xform.b = b;
xform.c = c;
xform.d = d;
xform.e = e;
xform.f = f;
return setTransform.call(ctx,a,b,c,d,e,f);
};
var pt = svg.createSVGPoint();
ctx.transformedPoint = function(x,y){
pt.x=x; pt.y=y;
return pt.matrixTransform(xform.inverse());
}
}
</script>
以上代码用于使用缩放。它仅用于缩放图像。
现在如何将以下代码合并到html5-canvas中 html文件包含java脚本和canvas.I需要缩放
我需要将缩放代码合并到下面的html代码中,以便我可以缩放cavas
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
canvas {
width: 1000;
height: 650;
border: 1px solid black;
background-image: url("http://www.geckosurfacing.com.au/wp-content/uploads/Cropped-1.jpg");
background-repeat: no-repeat;
}
</style>
</head>
<body onload="startGame(), bat()" >
<canvas id="mycanvas"></canvas>
<script >document.getElementById("mycanvas").style.backgroundImage = "http://www.geckosurfacing.com.au/wp-content/uploads/Cropped-1.jpg";</script>
<script>
function bat(){
function drawLine(startX, startY, endX, endY, lineWidth){
ctx.moveTo(startX, startY);
ctx.lineTo(endX, endY);
ctx.lineWidth = lineWidth;
ctx.stroke();
};
function drawCanvas(paddle1X, paddle1Y){
padLX = paddle1X;
padLY = paddle1Y;
drawLine(410, 520, paddle1X, paddle1Y, 5);
};
function drawPaddle1(paddle1X, paddle1Y){
padLX = paddle1X;
padLY = paddle1Y;
drawLine(410, 520, paddle1X, paddle1Y, 5);
}
var c = document.getElementById("mycanvas");
var ctx = c.getContext("2d");
//var padLX = 450;
//var padLY = 780;
//var padRX = 310;
//var padRY = 780;
drawCanvas(410, 520, 410, 1080); //Draw canvas size
document.addEventListener('keydown', function(event) {
//left
if((event.keyCode || event.which) == 90) {
ctx.save();
ctx.clearRect(0, 0, 600, 600);
ctx.restore();
ctx.beginPath();
drawPaddle1(510, 480);
}
});
document.addEventListener('keyup', function(event) {
//left
if((event.keyCode || event.which) == 90) {
ctx.save();
ctx.clearRect(0, 0, 600, 600);
ctx.restore();
ctx.beginPath();
drawPaddle1(350, 600);
}
});
}
</script>
<script >
var myGamePiece;
function startGame() {
myGamePiece = new component(30, 30, "red", 500,150);
myGameArea.start();
}
var myGameArea = {
canvas : document.getElementById("mycanvas"),
start : function() {
this.canvas.width = 1000;
this.canvas.height = 650;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.interval = setInterval(updateGameArea, 20);
},
stop : function() {
clearInterval(this.interval);
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
function component(width, height, color, x, y, type) {
this.type = type;
this.width = width;
this.height = height;
this.speed = 1;
this.angle = 10;
this.moveAngle = -1;
this.x = x;
this.y = y;
this.update = function() {
ctx = myGameArea.context;
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.angle);
ctx.fillStyle = color;
ctx.fillRect(this.width / -2, this.height / -2, this.width, this.height);
ctx.restore();
}
this.newPos = function() {
this.angle += this.moveAngle * Math.PI / 540;
this.x += this.speed * Math.sin(this.angle);
this.y -= this.speed * Math.cos(this.angle);
if(this.x >500){
this.speed = 1;
this.angle= 10;
this.moveAngle = -1;
this.angle += this.moveAngle * Math.PI / 90;
this.y +=this.speed * Math.cos(this.angle);
if (this.y > 500) {
myGameArea.stop();
}
console.log("done");
}
}
}
function updateGameArea() {
myGameArea.clear();
myGamePiece.newPos();
bat();
myGamePiece.update();
}
</script>
</body>
</html>