月度存档: 8 月 2013 - 第3页

canvas控制显示层级

[code]
/**
* …states and transforms
* @author amyflash.com
*/

(function() {
var can = document.getElementById(“canvas”);
var ctx = can.getContext(“2d”);
ctx.font=”20px Arial”;
ctx.fillText(“旋转”,20,20);

ctx.strokeStyle=”#000000″;
ctx.fillStyle=”#FF0000″;
ctx.lineWidth = “5”;

ctx.translate(400,100); //x:400,y:100
ctx.rotate(Math.PI/4);
ctx.save(); //控制显示层级

ctx.fillRect(50,50,200,200);
ctx.strokeRect(50,50,200,200);

ctx.scale(0.5,0.5);
ctx.fillStyle=”ffff00″;
ctx.rotate(Math.PI/6);
ctx.fillRect(50,50,200,200);
ctx.strokeRect(50,50,200,200);

//保证最先画的显示在最上层
ctx.restore();
ctx.fillRect(50,50,200,200);
ctx.strokeRect(50,50,200,200);
})();
[/code]

canvas画圆及弧

[code]
/**
* …画圆,半圆,弧形
* @author amyflash.com
*/

(function() {
var can = document.getElementById(“canvas”);
var ctx = can.getContext(“2d”);
ctx.font=”20px Arial”;
ctx.fillText(“画圆,半圆,弧形”,20,20);

ctx.strokeStyle=”#000000″;
ctx.fillStyle=”rgba(255,255,0,0.5)”;//”#FF0000″;
ctx.lineWidth = “5”;
//ctx.fillRect(50,50,200,200);
//ctx.strokeRect(50,50,200,200);

ctx.beginPath();
//ctx.arc(200,200,50,0,Math.PI*2);// 画正圆
//ctx.arc(200,200,50,0,Math.PI);// 画半圆
//画圆弧
ctx.moveTo(0,300);//起点
//ctx.quadraticCurveTo(0,0,300,0);//1个控制点和终点
ctx.bezierCurveTo(0,0,300,0,400,500,25,50);//多个控制点
ctx.fill();
ctx.stroke();

})();
[/code]

hello_canvas

[code]
/** demo.js
* …comparing canvas and BitmapData
* @author amyflash.com
*/

(function() {
var can = document.getElementById(“canvas”);//
var ctx = can.getContext(“2d”);
ctx.font=”20px Arial”;
ctx.fillText(“hello world”,50,50);
})();
[/code]
[

js也玩3d之webgl

threejs加载到html文件
[code]
/**
* …demo.js
* @author amyflash.com
*/

(function() {
var camera,scene,renderer,cube,material,mesh;
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight,1,10000);
camera.position.z = 1000;
scene.add(camera);
cube = new THREE.CubeGeometry(500,300,300);
//material = new THREE.MeshBasicMaterial({color:0x0000ff,wireframe:true});
material = new THREE.MeshLambertMaterial({color:0x0000ff});
mesh = new THREE.Mesh(cube,material);
scene.add(mesh);

var pointLight = new THREE.PointLight(0xffffff);
pointLight.position.x = 10;
pointLight.position.x = 50;
pointLight.position.x = 1000;
scene.add(pointLight);

//var dLight= new THREE.DirectionalLight(0xffffff);
//dLight.position.set(0,1,0);
//scene.add(dLight);

renderer = new THREE.CanvasRenderer();
renderer.setSize(window.innerHeight,window.innerHeight);
document.body.appendChild(renderer.domElement);

animate();

function animate(){
requestAnimationFrame(animate);
mesh.rotation.x+=0.02;
mesh.rotation.y+=0.02;
renderer.render(scene,camera);
}
})();
[/code]

WP-DBManager安装后出现红色警告的解决办法

http://www.shanbuling.com/2120.html

在线调试和演示的前端开发工具 – jsFiddle

在做flash开发的时候,我们经常需要测试下自己写的小函数,但是又不想新建个工程那么麻烦
所以,我经常是打开flashcc,然后在帧上面写代码测试,js里有没有类似方便的小函数测试工具呢,这儿推荐一个:
打开jsFiddle的网站:http://jsfiddle.net/,尼玛被墙了
只好找个开源实现放自个服务器上http://amyflash.duapp.com/jstool/
没有js窗口,只能写在html里测试,聊胜于无啦,如果有更好的欢迎推荐

利用modernizr来解决浏览器兼容问题

http://modernizr.com里有很详细的使用说明,但是经常会被墙掉,所以这儿弄个备份,download it!
基本用法:
[code]
/**
* …demo.js
* @author amyflash.com
*/

(function() {
if(Modernizr.canvas)
{
console.log(“canvas is isSupported”);
}
console.log(Modernizr);
})();
[/code]

面向对象的js探索3_引用外面定义的js类

As3里面有import,js里据说有require.js可以模拟,但我觉得最简单莫过于直接在html文件里贴script标签了
[code]










[/code]
~~~~~~~~~~~~~~~~~~~~~~
[code]
/**
* …Person.js
* @author amyflash.com
*/

(function(window) {
function Person(name,address){
this.name=name;
this.address = address;
}

Person.prototype.sayHello = function()
{
console.log(this.name+” says hello “+Person.BUDONG);
};

Person.BUDONG=”static var”;

window.Person = Person;
})(window);
[/code]
~~~~~~~~~~~~~~~~~~~~~~~~~~
[code]
/**
* …demo.js
* @author amyflash.com
*/

(function() {

/*function Person(name,address){
this.name=name;
this.address = address;
}

Person.prototype.sayHello = function()
{
console.log(this.name+” says hello “+Person.BUDONG);
};

Person.BUDONG=”static var”;*/
var lee = new Person(“amyflash”,”shanghai”);
lee.sayHello();
})();
[/code]
注意window.Person = Person;自执行匿名函数里加上了window变量,是为了让外面的js文件可以找到这个Person类

面向对象的js探索2

修改demo.js代码
[code]
/**
* …
* @author amyflash.com
*/

(function() {

function Person(name,address){
this.name=name;
this.address = address;
}

Person.prototype.sayHello = function()
{
console.log(this.name+” says hello”);
};

Person.BUDONG=”I’m static var”;
var lee = new Person(“amyflash”,”shanghai”);
lee.sayHello();
})();
[/code]
知识点解释:
1.Person类是一个dynamic类型的类,可以在外面用.prototype.function的形式动态添加成员方法和变量,注意sayHello函数里的name前面加了个this.
2. Person.BUDONG=”I’m static var”;申明并赋值了一个Person类的静态变量

面向对象的js探索1

推荐用flashdevelop生成demo.html和demo.js文件,会自动写好通用的东西,上代码:
[code]










[/code]
~~~~~~~~~~~~~~~~~我是分割线~~~~~~~~~~~~~~~~~
[/code]
[code]
/**
* …demo.js
* @author amyflash.com
*/

(function() {

function Person(name,address){
this.name=name;
this.address = address;

this.sayHello = function(){
console.log(name+” says hello”);
};
}

var lee = new Person(“amyflash”,”shanghai”);
lee.sayHello();

})();
[/code]
浏览器里打开demo.html,控制台会输出
amyflash says hello
知识点解释:
1.(function() {
})();
这玩意就是一个js的自动执行自身代码的匿名函数,可以理解为as3的入口函数
2.定义了一个Person类,其构造函数有两个参数name,address
Person类里用this.的方式定义了两个public类型的成员变量和一个public类型的成员方法sayHello
3.lee是Person类的一个实例