This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 使用moveTo()、lineTo()和closePath() 方法绘制规则多边形 | |
// 定义一个以(x, y) 为中心,半径为r 的规则n 边行 | |
// 每个定点都是均匀分布在圆周上 | |
// 将第一个顶点放置在最上面,或者指定一定角度 | |
// 除非最后一个参数是true,否则顺时针旋转 | |
function polygon(c, n, x, y, r, angle, counterclockwise){ | |
angle = angle || 0; | |
counterclockwise = counterclockwise || false; | |
c.moveTo(x + r*Math.sin(angle), // 从第一个定点开始一条新的子路径 | |
y - r*Math.cos(angle)); // 使用三角法计算位置 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>PieChart</title> | |
<script src="PieChart.js"></script> | |
</head> | |
<body onload="document.body.appendChild( | |
pieChart([12, 23, 34, 45], 640, 400, 200, 200, 150, | |
['red', 'blue', 'yellow', 'green'], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* rollover.js: 优雅的图片翻转实现方式 | |
* | |
* 要创建图片翻转效果,将此模块引入到HTML 文件中 | |
* 然后在任意<img> 元素上使用data-rollover 属性来指定翻转图片的URL 即可 | |
* 如下所示: | |
* | |
* <img src="normal_image.png" data-rollover="rollover_image.png"> | |
* | |
* 要注意的是,此模块依赖与onLoad.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CACHE MANIFEST | |
# PermaNote v8 | |
permanote.html | |
permanote.js | |
NETWORK: | |
note |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 以名/值的形式存储cookie | |
// 同时采用encodeURIComponent() 函数进行编码,来转义分号、逗号和空白符 | |
// 如果daysToLive 是一个数字,设置max-age 属性为该数值表示cookie 直到指定的天数 | |
// 到了才会过期。如果daysToLive 是0 就表示删除cookie | |
function setcookie(name, value, daysToLive){ | |
var cookie = name + "=" + encodeURIComponent(value); | |
if(typeof daysToLive === "number") | |
cookie +="; max-age=" + (daysToLive*60*60*24); | |
document.cookie = cookie; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 用XMLHttpRequest 模拟EventSource | |
// 在不支持EventSource API 的浏览器里进行模拟 | |
// 需要有一个XMLHttpRequest 对象在新数据写到长期存在的HTTP 连接中时发送readystatechange 事件 | |
// 注意,这个API 的实现是不完整的 | |
// 它不支持readyState 属性、close() 方法、open 和error 事件 | |
// 消息事件也是通过onmessage 属性注册的--这个版本还没有定义addEventListener() 方法 | |
if(window.EventSource === undefined){ // 如果未定EventSource 对象 | |
window.EventSource = function(url){ // 像这样进行模拟 | |
var xhr; // HTTP 连接器 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 根据指定的URL 发送一个JSONP 请求 | |
// 然后把解析得到的响应数据传递给回调函数 | |
// 在URL 中添加一个名为jsonp 的查询参数,用于指定该请求的回调函数的名称 | |
function getJSONP(url, callback){ | |
// 为本次请求创建一个唯一的回调函数名称 | |
var cbnum = "cb" + getJSONP.counter ++; // 每次自增计数器 | |
var cbname = "getJSONP." + cbnum; // 作为JSONP 函数的属性 | |
// 将回调函数名称以表单编码的形式添加到URL 的查询部分中 | |
// 使用jsonp 作为参数名,一些支持JSONP 的服务 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* linkdetail.js | |
* | |
* 这个常见的JavaScript 模块查询有href 属性但没有title 属性的所有<a> 元素 | |
* 并给它们注册onmouseover 事件处理程序 | |
* 这个事件处理程序使用XMLHttpRequest HEAD 请求取得链接资源的详细信息 | |
* 然后把这些详细信息设置为链接的title 属性 | |
* 这样它们将会在工具提示中显示 | |
*/ | |
whenReady(function(){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 发起HTTP GET 请求获取指定URL 的内容 | |
// 如果响应成功到达,传入responseText 给回调函数 | |
// 如果响应在timeout 毫秒内没有到达,中止这个请求 | |
// 浏览器可能在abort() 后触发"readystatechange" | |
// 如果是否部分请求结果到达,甚至可能设置status 属性 | |
// 所以需要设置一个标记,当部分且超时的响应到达时不会调用回调函数 | |
// 如果使用load 事件就没有这个风险 | |
function timedGetText(url, timeout, callback){ | |
var request = new XMLHttpRequest(); // 创建新对象 | |
var timedout = false; // 是否超时 |