用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 相关文章推荐
contains和compareDocumentPosition 方法来确定是否HTML节点间的关系
Sep 13 Javascript
JavaScript中判断函数、变量是否存在
Jun 10 Javascript
js+css实现的圆角边框TAB选项卡滑动门代码分享(2款)
Aug 26 Javascript
微信支付如何实现内置浏览器的H5页面支付
Sep 25 Javascript
jQuery操作基本控件方法实例分析
Dec 31 Javascript
简单实现node.js图片上传
Dec 18 Javascript
vue2导航根据路由传值,而改变导航内容的实例
Nov 10 Javascript
使用use注册Vue全局组件和全局指令的方法
Mar 08 Javascript
实例分析javascript中的异步
Jun 02 Javascript
Vue页面跳转传递参数及接收方式
Sep 09 Javascript
js基于canvas实现时钟组件
Feb 07 Javascript
vue 使用饿了么UI仿写teambition的筛选功能
Mar 01 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与MySQL开发的8个技巧小结
2010/12/17 PHP
php whois查询API制作方法
2011/06/23 PHP
php 短链接算法收集与分析
2011/12/30 PHP
Thinkphp搭建包括JS多语言的多语言项目实现方法
2014/11/24 PHP
php上传图片客户端和服务器端实现方法
2015/03/30 PHP
php把时间戳转换成多少时间之前函数的实例
2016/11/16 PHP
yii2.0整合阿里云oss的示例代码
2017/09/19 PHP
php安装扩展mysqli的实现步骤及报错解决办法
2017/09/23 PHP
JS应用正则表达式转换大小写示例
2014/09/18 Javascript
javascript动态创建链接的方法
2015/05/13 Javascript
BOM系列第一篇之定时器setTimeout和setInterval
2016/08/17 Javascript
nodejs个人博客开发第一步 准备工作
2017/04/12 NodeJs
bootstrap的常用组件和栅格式布局详解
2017/05/02 Javascript
Angular中$state.go页面跳转并传递参数的方法
2017/05/09 Javascript
JS开发常用工具函数(小结)
2019/07/04 Javascript
python网络爬虫之如何伪装逃过反爬虫程序的方法
2017/11/23 Python
使用python爬虫实现网络股票信息爬取的demo
2018/01/05 Python
自学python的建议和周期预算
2019/01/30 Python
pyinstaller参数介绍以及总结详解
2019/07/12 Python
Python  word实现读取及导出代码解析
2020/07/09 Python
django rest framework 自定义返回方式
2020/07/12 Python
python正则表达式 匹配反斜杠的操作方法
2020/08/07 Python
K近邻法(KNN)相关知识总结以及如何用python实现
2021/01/28 Python
中国高端家电购物商城:顺电
2018/03/04 全球购物
世界上最大的隐形眼镜商店:1-800 Contacts
2018/11/03 全球购物
说出数据连接池的工作机制是什么?
2013/04/19 面试题
EntityManager都有哪些方法
2013/11/01 面试题
翻译专业应届生求职信
2013/11/23 职场文书
公司部门司机岗位职责
2014/01/03 职场文书
2014年寒假社会实践活动心得体会
2014/04/07 职场文书
环保建议书600字
2014/05/14 职场文书
卫生保健工作总结2015
2015/05/18 职场文书
2016年清明节红领巾广播稿
2015/12/17 职场文书
各类场合主持词开场白范文集锦
2019/08/16 职场文书
创业开店,这样方式更合理
2019/08/26 职场文书
MySql数据库触发器使用教程
2022/06/01 MySQL