基本上我想做的是:
hand.svg
和svg.get('name')
)head.svg
这里的主要思想是定义一次并使用存储在单例对象中的SVG多次绘制它们。
hand.svg
),但不显示第二个SVG元素(hand.svg
)[更新]
清除缓存并关闭两个浏览器后,当我重新打开项目页面时:
head.svg
),而不显示第一个元素(head.svg
)head.svg
),但不显示第二个元素(// Game canvas
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
// Canvas fullscreen
canvasFullscreen = function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Adapt fullscreen on resize
window.onresize = function(event) {
canvasFullscreen();
};
};
canvasFullscreen();
// SVG draw
var svg = new function() {
// The list of SVGs
this.get = {};
// Create Image & add to list
this.add = function(name, svg) {
this.get[name] = new Image;
this.get[name].src = svg;
};
// Draw SVG in Canvas
this.draw = function(name, svg, x, y, w, h) {
// Check if previously defined / added to list
if (typeof this.get[name] == 'undefined') {
// If SVG file defined, add it to list
if (svg !== false) this.add(name, svg);
}
// Draw image
ctx.drawImage(this.get[name], x, y, w, h);
};
}
// Draw head
svg.draw('head', 'https://richardev.com/public/head.svg', 30, 0, 50, 50);
// Draw hand
svg.draw('left_hand', 'https://richardev.com/public/hand.svg', 0, 12.5, 25, 25);
)
*,
*:before,
*:after {
margin: 0;
padding: 0;
border: 0;
outline: 0;
cursor: none;
}
html,
body {
overflow: hidden;
}
#window {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 10;
}
#canvas {
position: relative;
z-index: 20;
}
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Game</title>
</head>
<body>
<section id="window">
<canvas id="canvas" width="800" height="600"></canvas>
</section>
</body>
</html>
CreateNewRandomString
这里可能是什么问题? 预先感谢!