基础中的基础。CANVAS step01
每天书写一点点。 构思更多新概念。
var log = console.log, Pt = function(){ var Pt = function(id, type, width, height){ return new Pt.fn.init(id, type, width, height); }, _proto = Object.prototype, _toString = _proto.toString, _proto = Array.prototype, _forEach = _proto.forEach || function(db, callback){ for(var i = 0; i < db.length; i++){ callback.call(db[i], i, db); } }, _forIn = function(db, callback, args){ for( var i in db){ callback.call(db[i],i, db[i], args || db); } }, _listProperties = function (o){ var objectToInspect, result = []; for(objectToInspect = o; objectToInspect !== null; objectToInspect = Object.getPrototypeOf(objectToInspect)){ result = result.concat(Object.getOwnPropertyNames(objectToInspect)); } return result; }, _type = function(data, type){ return _toString.call(data)==='[object '+ type +']'; }, fns = {}; Pt.define = function (name, value){ if(name){ if(_type(name, 'Object') && !value){ try{ return Object.defineProperties(this, name); } catch(e){ throw new Error(e); } } else if(_type(name, 'String') && _type(value, 'Object')){ try{ return Object.defineProperty(this, name, value); } catch(e){ throw new Error(e); } } else{ throw new Error("UnExpected type error! Unknown type in "+ name + "and "+ value); } } throw new TypeError("UnExpected NullException in name and value!"); }; Pt.prototype = fns; fns.constructor = Pt; Pt.define.call(fns,{ 'init': { value: function(id, type, width, height){ if(!!id && _type(id, 'String')){ this.__info__(id, type || '2d', width, height); } return this; }, writable: false, enumerable:false, configurable:false }, 'define': { value: Pt.define, writable: false, enumerable: true, configurable: false }, '__info__':{ value: function(id, type, width, height){ var node; if(type && _type(type, 'String')){ node = document.getElementById(id); width = width || node.offsetWidth; height = height || node.offsetHeight; this.node = node; this.ctx = node.getContext(type); this.width = width; this.height = height; return this; } else{ if( id in this._cache){ return this._cache[id]; } return this; } }, writable: false, enumerable: false, configurable: false }, '__layerId2Name__':{ value: {}, enumerable: false, writable:true, configurable:false }, '__layerName2Id__':{ value: {}, enumerable: false, writable:true, configurable:false }, '__layerIndex__':{ value: 0, enumerable: false, writable:true, configurable:false }, 'layer': { value: function(name, handle, args){ handle = _type(handle, 'Function') ? handle : (_type(handle, 'String') && handle in this) ? this[handle] : null; if(_type(name, 'String') && handle){ if(name in this.__layerName2Id__){ throw new Error("name has be defined!"); } this.__layerId2Name__[this.__layerIndex__] = name; this.__layerName2Id__[name] = { cId: this.__layerIndex__++, handle: handle, args: args }; this.define('__layerIndex__', { value: this.__layerIndex__, enumerable: false, writable: true, configurable:false }) handle.apply(this, args); } else if(_type(name, 'Object') && !handle){ _forIn(name, function(i, item, me){ me.layer.call(me, i, item.handle, item.args); }, this); } else{ throw new Error('TypeError! Unknown Type with name and handle!'); } return this; }, writable: false, enumerable: true, configurable: false }, 'removeLayer': { value: function(name){ var layer, cId; if(name in this.__layerName2Id__){ layer = this.__layerName2Id__[name]; cId = layer.cId; if(cId in this.__layerId2Name__){ // remove id2name delete this.__layerId2Name__[cId]; //clean arc this.ctx.clearRect(0,0, this.width, this.height); } else{ throw new Error("Can't find the layer in here! Did u make sure save it in layer or haven't removed?"); } delete this.__layerName2Id__[name]; _forIn(this.__layerName2Id__, function(i, item, me){ this.handle.apply(me, this.args); }, this); } else{ throw new Error('Name is not in layer'); } } } }); Pt.define({ 'fn':{ value: fns, writable: true, configurable: false, enumerable: true } }); Pt.fn.init.prototype = Pt.fn; return Pt; }(); Pt.fn.define("line",{ value: function(ox,oy,ex,ey){ this.ctx.fillStyle="red"; this.ctx.beginPath(); this.ctx.moveTo(ox, oy); this.ctx.lineTo(ex, ey); this.ctx.bezierCurveTo(ox + 30, oy + 30, ox + 30, oy + 30, ex+ 20, ey+ 100); this.ctx.lineTo(ox, oy); this.ctx.fill(); }, enumerable: true, writable: false, configurable: false }); var pt = Pt('cvs'); /*pt.line(0,0,200,90);*/ pt.layer('01', 'line', [0,0,200,90]); pt.layer({ '03': { handle: 'line', args: [0,2,10,50] }, '02': { handle: 'line', args: [30,40,150,500] } }); pt.removeLayer('01'); pt.removeLayer('02');
目前可以简单的实现这些功能了。。
增 删 记录层。
也许明天会更好。
开始 到结束 只是那么个过程。结束了 就忘记吧。
分类:
javascript learn
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库