the below code gives "Uncaught TypeError: Cannot call method 'getContext' of undefined". The code is my experiment to port a canvas app using module pattern as described here.
Please find the same code in jsfiddle, which works fine.
thanks.
//====test.html ========
<html>
<head>
<title>Location</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
<canvas id="cvs" height="600" width="800"></canvas>
</body>
</html>
//==test.js ========
if (typeof (NS) === 'undefined') {
NS = {};
}
NS.AppName = (function ($) {
// Private members
/*var _first = function () {
},
_second = function () {
},*/
var _cvs = $("#cvs");
var _ctx = _cvs.get(0).getContext("2d");
var _privateVar = "privateVar: accessed from within AppLaunch.Admin namespace";
// Private Methods
var _privateMethod = function () {
log("privateMethod: accessed only from within AppLaunch.Admin");
}; // Last variable in a chain must always end with ; before the return {}
function log() {
if (window.console && window.console.log)
window.console.log('[AppName] ' + Array.prototype.join.call(arguments, ' '));
};
return {
init: function () {
},
// Public
myPublicMethod: function() {
//alert("myPublicMethod" + _cvs.height());
_ctx.fillRect(25,25,100,100);
_ctx.clearRect(45,45,60,60);
_ctx.strokeRect(50,50,50,50);
}
}
})(jQuery);
// Initialize
jQuery().ready(function() {
NS.AppName.init()
NS.AppName.myPublicMethod();
});