用javascript实现画板的代码


Posted in Javascript onSeptember 05, 2007

在控制台中输入 
db.drawCircle([50,50],20,"black"); 
db.drawLine([5,5],[36,44],"red"); 
可以看到效果 

<body style="margin:0px;">  
</body>  
<script>  
    function DrawingBoard(width,height,size)  
    {  
        size=size||3  
        var container=document.createElement("div");  
        this.container=container;          container.runtimeStyle.width=(width)*size+"px";  
        container.runtimeStyle.height=(height)*size+"px";  
        container.runtimeStyle.margin="0px";  
        //container.style.border="solid 1px blue";  
        var count=0;  
                for(var y=0;y<height;y++)  
        {  
            for(var x=0;x<width;x++)  
            {  
                var curr=document.createElement("div");  
                curr.runtimeStyle.height=size+"px";  
                curr.runtimeStyle.width=size+"px";  
                curr.runtimeStyle.display="inline";  
                curr.runtimeStyle.overflow="hidden";  
                curr.style.backgroundColor="green";  
                curr.src="";  
                container.appendChild(curr);  
            }  
        }  
                //alert(curr.currentStyle.display);  
        //document.body.appendChild(container);  
        this.drawLine=function(start,end,color)  
        {  
            var dx=start[0]-end[0];  
            var dy=start[1]-end[1];  
            var x,y;  
            if( Math.abs(dx) > Math.abs(dy) )  
            {  
                for(var x=start[0];x!=end[0]+(end[0]-start[0])/Math.abs(end[0]-start[0]);x+=(end[0]-start[0])/Math.abs(end[0]-start[0]) )  
                {  
                    y=Math.round((x-start[0])/dx*dy+start[1]);  
                    this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;  
                }  
            }  
            else  
            {  
                for(var y=start[1];y!=end[1]+(end[1]-start[1])/Math.abs(end[1]-start[1]);y+=(end[1]-start[1])/Math.abs(end[1]-start[1]) )  
                {  
                    x=Math.round((y-start[1])/dy*dx+start[0]);  
                    this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;  
                }  
            }  
        }  
        this.drawCircle=function(m,R,color)  
        {  
            for(var r=0;r<=Math.floor(Math.sqrt(R*R-r*r));r++)  
            {  
                x=m[0]+r;y=m[1]+Math.floor(Math.sqrt(R*R-r*r));  
                this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;  
                x=m[0]-r;y=m[1]+Math.floor(Math.sqrt(R*R-r*r));  
                this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;  
                x=m[0]+r;y=m[1]-Math.floor(Math.sqrt(R*R-r*r));  
                this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;  
                x=m[0]-r;y=m[1]-Math.floor(Math.sqrt(R*R-r*r));  
                this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;  
                y=m[1]+r;x=m[0]+Math.floor(Math.sqrt(R*R-r*r));  
                this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;  
                y=m[1]-r;x=m[0]+Math.floor(Math.sqrt(R*R-r*r));  
                this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;  
                y=m[1]+r;x=m[0]-Math.floor(Math.sqrt(R*R-r*r));  
                this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;  
                y=m[1]-r;x=m[0]-Math.floor(Math.sqrt(R*R-r*r));  
                this.container.childNodes[this.trans([x,y])].style.backgroundColor=color;  
            }  

        }  
        this.appendto=function(parent)  
        {  
            parent.appendChild(this.container);  
        }  
        this.drawPoint=function(p,color)  
        {  
            this.container.childNodes[this.trans(p)].style.backgroundColor=color;  
        }  
        this.trans=function(p)  
        {  
            return p[0]+p[1]*width;  
        }  
        container=null;  
    }  
    function Console(width,height,command)  
    {  
        var container=document.createElement("div");  
        this.container=container;  
        container.runtimeStyle.width=(width);  
        container.runtimeStyle.height=(height);  
        container.runtimeStyle.margin="0px";  
        container.runtimeStyle.backgroundColor="black";  
        container.runtimeStyle.fontFamily="Terminal";  
        container.runtimeStyle.color="white";  
        container.runtimeStyle.fontSize="16px";  
        this.output=document.createElement("div");  
        container.appendChild(this.output);  
        container.innerHTML+="js>"  
        this.input=document.createElement("input");  
        container.appendChild(this.input);  
        this.input.runtimeStyle.backgroundColor="black";  
        this.input.runtimeStyle.borderWidth="0px";  
        this.input.runtimeStyle.color="white";  
        this.input.runtimeStyle.fontFamily="Terminal";  
        this.input.runtimeStyle.width="90%"  
        this.input.runtimeStyle.fontSize="16px"  
        this.input.runtimeStyle.position="relative";  
        this.input.runtimeStyle.top="2px";  
        command=command||function(str)  
        {  
            var e;  
            try{  
                var r=eval(str);  
            } catch(e) {  
                return "Bad command";  
            }  
            return r;  
        }  
        this.run=function(str)  
        {  
            this.input.parentNode.childNodes[0].innerHTML+=str+'<br/>'  
            this.input.parentNode.childNodes[0].innerHTML+=(command(str)+"<br/>")  
        }  
        this.input.command=function()  
        {  
            this.parentNode.childNodes[0].innerHTML+=this.value+'<br/>'  
            this.parentNode.childNodes[0].innerHTML+=(command(this.value)+"<br/>")  
        }  
        this.input.onkeyup=new Function("e","e=e||event;if(e.keyCode!=13)return;this.command();this.value='';");  
        this.appendto=function(parent)  
        {  
            parent.appendChild(this.container);  
        }  
        container=null;  
    }  
    var c=new Console("100%","50%");  
    c.appendto(document.body);  
    c.run("window.db=new DrawingBoard(100,100);document.body.appendChild(db.container);");  
</script>
Javascript 相关文章推荐
Javascript - HTML的request类
Jan 09 Javascript
腾讯的ip接口 方便获取当前用户的ip地理位置
Nov 25 Javascript
裁剪字符串trim()自定义改进版
Apr 10 Javascript
js调用后台servlet方法实例
Jun 09 Javascript
JS文本框默认值处理详解
Jul 10 Javascript
浅谈react受控组件与非受控组件(小结)
Feb 09 Javascript
对vue里函数的调用顺序介绍
Mar 17 Javascript
vue-cli 3.x 修改dist路径的方法
Sep 19 Javascript
javascript中一些奇葩的日期换算方法总结
Nov 14 Javascript
js对象简介与基本用法示例
Mar 13 Javascript
vue-cli3自动消除console.log()的调试信息方式
Oct 21 Javascript
解决Vue+SpringBoot+Shiro跨域问题
Jun 09 Vue.js
js中的escape及unescape函数的php实现代码
Sep 04 #Javascript
一个符号插入器 中用到的js代码
Sep 04 #Javascript
【消息提示组件】,兼容IE6/7&amp;&amp;FF2
Sep 04 #Javascript
一个用js实现控制台控件的代码
Sep 04 #Javascript
科讯商业版中用到的ajax空间与分页函数
Sep 02 #Javascript
PNGHandler-借助JS让PNG图在IE下实现透明(包括背景图)
Aug 31 #Javascript
给Javascript数组插入一条记录的代码
Aug 30 #Javascript
You might like
PHP基于递归实现的约瑟夫环算法示例
2017/08/27 PHP
jquery div 居中技巧应用介绍
2012/11/24 Javascript
javascript实现的多个层切换效果通用函数实例
2015/07/06 Javascript
谈谈对offsetleft兼容性的理解
2015/11/11 Javascript
基于jquery实现轮播焦点图插件
2016/03/31 Javascript
AngularJS出现$http异步后台无法获取请求参数问题的解决方法
2016/11/03 Javascript
使用nodejs下载风景壁纸
2017/02/05 NodeJs
Angular 4.0学习教程之架构详解
2017/09/12 Javascript
php中and 和 &amp;&amp;出坑指南
2018/07/13 Javascript
layui 给数据表格加序号的方法
2018/08/20 Javascript
vue中slot(插槽)的介绍与使用
2018/11/12 Javascript
checkbox在vue中的用法小结
2018/11/13 Javascript
javascript如何实现create方法
2019/11/04 Javascript
小程序卡片切换效果组件wxCardSwiper的实现
2020/02/13 Javascript
Vue实现移动端拖拽交换位置
2020/07/29 Javascript
[40:12]Liquid vs Chaos 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/16 DOTA
python实现每次处理一个字符的三种方法
2014/10/09 Python
教你用Type Hint提高Python程序开发效率
2016/08/08 Python
python获取微信小程序手机号并绑定遇到的坑
2018/11/19 Python
详解配置Django的Celery异步之路踩坑
2018/11/25 Python
Python 判断图像是否读取成功的方法
2019/01/26 Python
pandas删除指定行详解
2019/04/04 Python
使用python接入微信聊天机器人
2020/03/31 Python
用pyqt5 给按钮设置图标和css样式的方法
2019/06/24 Python
倩碧美国官网:Clinique美国
2016/07/20 全球购物
我的applet原先好好的, 一放到web server就会有问题,为什么?
2016/05/10 面试题
中学门卫岗位职责
2013/12/26 职场文书
医院学雷锋活动策划方案
2014/02/15 职场文书
团队口号大全
2014/06/06 职场文书
教师求职自荐书
2014/06/14 职场文书
西柏坡导游词
2015/02/05 职场文书
培训感想范文
2015/08/07 职场文书
Vue.js 带下拉选项的输入框(Textbox with Dropdown)组件
2021/04/17 Vue.js
pycharm 如何查看某一函数源码的快捷键
2021/05/12 Python
世界十大动漫制作公司排行榜,迪士尼上榜,第二是美国代表性文化符
2022/03/18 欧美动漫
Vue2项目中对百度地图的封装使用详解
2022/06/16 Vue.js