/**
* Version 1.1, May 4: fixed issue with symbols in library folders.
**/
/**
* BitmapSlice9 JSFL by Grant Skinner. Apr 13, 2010
* Visit www.gskinner.com/blog for documentation, updates and more free code.
*
*
* Copyright (c) 2010 Grant Skinner
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
**/ var doc = fl.getDocumentDOM();
var minAsVersion = 2;
var minPlayerVersion = 8;
var selection = null;
var layer = null;
var grid = null; run(); function run() {
if (!checkDocument()) { return; }
if (!checkSelection()) { return; }
if (!checkScale9()) { return; } // everything checks out.
slice();
} function checkDocument() {
if (doc == null) {
alert('You must have an FLA open as your active document to run this command.');
return false;
}
if (doc.asVersion < minAsVersion || parseInt(doc.getPlayerVersion()) < minPlayerVersion) {
alert('The Scale9 feature must target Flash Player ' + minPlayerVersion + ' and ActionScript ' + minAsVersion + '.');
return false;
}
return true;
} function checkSelection() {
var selectedItems = doc.selection;
if (selectedItems.length != 1 || selectedItems[0].instanceType != "bitmap") {
alert('Please select a single bitmap object on the stage before running this command.');
return false;
} var selectedFrames = doc.getTimeline().getSelectedFrames();
if (selectedFrames.length != 3) {
alert('Only a single keyframe should be selected before running this command.');
return false;
}
selection = selectedItems[0];
layer = doc.getTimeline().layers[selectedFrames[0]];
frame = layer.frames[selectedFrames[1]];
return true;
} function checkScale9() {
var item = doc.getTimeline().libraryItem; //CS5
if (item == null) {
doc.library.selectItem(doc.getTimeline().name,true,true);
item = doc.library.getSelectedItems()[0];
}
if (item == null) {
alert('The selected bitmap must be in a MovieClip symbol.');
return false;
}
if (!item.scalingGrid) {
if (confirm('Scale 9 must be enabled for the parent symbol. Would you like to enable this now?')) {
item.scalingGrid = true;
}
return false;
}
grid = item.scalingGridRect;
grid = {x:grid.left, y:grid.top, width:grid.right-grid.left, height:grid.bottom-grid.top};
return true;
} function slice() {
var timeline = doc.getTimeline();
var bmp = selection.libraryItem;
var name = bmp.name;
if (name.indexOf(".") > 0) { name = name.substr(0,name.indexOf(".")); }
if (name.indexOf("/") > 0) { name = name.substr(name.lastIndexOf("/")+1); }
var sliceLayer = null;
var bmpLayer = null;
var index; // check if our selection is already on an existing _bmp layer:
if (layer.name.substr(-4) == "_bmp") {
bmpLayer = layer;
name = layer.name.substr(0,layer.name.length-4);
} // check if the slice layer already exists:
var sliceLayerIndexes = timeline.findLayerIndex(name+"_slices");
if (sliceLayerIndexes != null && sliceLayerIndexes.length > 0) {
sliceLayer = timeline.layers[sliceLayerIndexes[0]];
} // create or rename bmpLayer if needed:
if (bmpLayer == null) {
// create a bmpLayer if there are other elements on the current layer.
if (frame.elements.length > 1) {
// create new layer:
doc.clipCut();
if (sliceLayer) { timeline.setSelectedLayers(sliceLayerIndexes[0]); }
index = timeline.addNewLayer(name+"_bmp","guide",true);
bmpLayer = timeline.layers[index];
doc.clipPaste(true);
} else {
// rename the current layer:
layer.name = name+"_bmp";
bmpLayer = layer;
}
} // set up bmpLayer properties:
bmpLayer.visible = false; // hidden
bmpLayer.layerType = "guide"; // avoid compiling it into the SWF if (sliceLayer) {
// sliceLayer already exists, clear old slices:
if (selectSlices(sliceLayer)) { document.deleteSelection(); }
} else {
// create new sliceLayer below the bmpLayer:
index = timeline.addNewLayer(name+"_slices","normal",false);
sliceLayer = timeline.layers[index];
} // ensure the sliceLayer is selected:
timeline.setSelectedLayers(timeline.findLayerIndex(sliceLayer.name)[0]); // find the original size and path of the library bitmap
var bmpWidth = selection.hPixels;
var bmpHeight = selection.vPixels;
var bmpPath = bmp.name; // do the slicing:
var srcRect = {x:selection.x, y:selection.y, width:selection.width, height:selection.height}; var cols = [grid.x-10000, grid.x, grid.x+grid.width, 10000, grid.width, 10000];
var rows = [grid.y-10000, grid.y, grid.y+grid.height, 10000, grid.height, 10000]; for (var row=0; row<3; row++) {
for (var col=0; col<3; col++) {
var targetRect = getIntersection(srcRect, {x:cols[col], y:rows[row], width:cols[col+3], height:rows[row+3]});
drawRect(bmpPath, bmpWidth, bmpHeight, srcRect, targetRect);
}
} selectSlices(sliceLayer);
} function selectSlices(sliceLayer) {
var elements = sliceLayer.frames[0].elements;
var s = [];
for (var i=0; i<elements.length; i++) {
// only remove rectangle primitives, in case there are other items on the layer:
if (elements[i].elementType == "shape" && elements[i].isRectangleObject) {
s.push(elements[i]);
}
}
if (s.length > 0) {
document.selection = s;
}
return s.length > 0;
} function drawRect(bmpPath, bmpWidth, bmpHeight, srcRect, targetRect) {
if (targetRect == null) { return; }
var fill = doc.getCustomFill();
fill.style = "bitmap";
fill.bitmapIsClipped = false;
fill.bitmapPath = bmpPath;
var matrix = selection.matrix;
matrix.tx = srcRect.x;
matrix.ty = srcRect.y;
matrix.a = srcRect.width/(bmpWidth/20); // 20 seems to be a magic number for calculating the matrix.
matrix.d = srcRect.height/(bmpHeight/20);
matrix.b = matrix.c = 0;
fill.matrix = matrix;
doc.setCustomFill(fill);
doc.addNewPrimitiveRectangle({left:targetRect.x, top:targetRect.y, right:targetRect.x+targetRect.width, bottom:targetRect.y+targetRect.height},0,false,true);
} function getIntersection(rect1, rect2) {
var x = max(rect1.x, rect2.x);
var y = max(rect1.y, rect2.y);
var r = min(rect1.x+rect1.width, rect2.x+rect2.width);
var b = min(rect1.y+rect1.height, rect2.y+rect2.height);
if (r > x && b > y) {
return {x:x, y:y, width:r-x, height:b-y}
}
return null;
} function max(num1, num2) {
return (num1 > num2) ? num1 : num2;
}
function min(num1, num2) {
return (num1 < num2) ? num1 : num2;
}

  

位图9宫格 BitmapSlice9.jsfl的更多相关文章

  1. 宫格布局实例(注意jquery的版本号要统一)

    <!DOCTYPE html><html><head><meta charset="utf-8" /><style> * ...

  2. 宫格布局实例(注意jquery的版本号要统一)2

    <!DOCTYPE html><html><head><meta charset="utf-8" /><style> * ...

  3. css-九宫格自适应的实现

    高度自适应使用padding 或 padding-bottom + 百分比来实现: 宽度自适应使用width + 百分比来实现. 下面是实现九宫格自适应的代码: <!DOCTYPE html&g ...

  4. HTML5 Canvas中9宫格的坑

    近期小鸟情人游戏上了手机qq空间,一个3岁的游戏来了她的第二春.为了能有更好的表现,我们对其进行了一次改版. 改版当中一项就是对原来的弹出框样式进行改进.将大块木板材质改成纯色(边框为圆角金属材质)样 ...

  5. 微信小程序多宫格抽奖

    最近闲来无事,做了一个多宫格抽奖的例子,有什么需要改进或者错误的地方,请留言,谢谢 首先看效果 思路是先让其转动2圈多,然后再进行抽奖,格子运动用的是setTimeout,让其运行的时间间隔不一样,然 ...

  6. Android自定义多宫格解锁控件

    在此之前,一直在想九宫格的实现方法,经过一个上午的初步研究终于完成了一个简单的N*N的宫格解锁组件,代码略显粗糙,仅仅做到简单的实现,界面等后期在做优化,纯粹是学习的目的,在算法上有点缺陷,如果有错误 ...

  7. vue 如何拿到后台传回的富文本中的img,进行9宫格排列展示以及相关处理

    描述: res.data.list 返回的数组, 数组中的每个对象有一个 content,就是传回来的富文本的内容,要拿到这里面的所有的img,进行9宫格排列处理: 1.let img = this. ...

  8. golang 六宫格、九宫格头像生成

    图片示例就不传了,在原WordPress上. //Merge6Grid 6宫格 //rule NO1:至少3张图 最多6张图 // NO2:第一张大小 60*60 其他大小 28*28 间隔4px 合 ...

  9. Python爬虫学习笔记之微信宫格验证码的识别(存在问题)

    本节我们将介绍新浪微博宫格验证码的识别.微博宫格验证码是一种新型交互式验证码,每个宫格之间会有一条 指示连线,指示了应该的滑动轨迹.我们要按照滑动轨迹依次从起始宫格滑动到终止宫格,才可以完成验证,如 ...

随机推荐

  1. Real-Time Rendering 3 彩图

    电子版只有黑白图,彩图见官网链接:http://www.realtimerendering.com/book.html.虽然只有部分彩图,不过够用了,下面是其中几幅图,如果只能看黑白的,那得多蛋疼:

  2. Linux学习笔记(6)-工作管理

    什么是工作管理 工作来自job命令的翻译,job命令可以查看后台工作的进程.举例来说什么是工作管理,当你要打包一个比较大的目录时,很耗时间,但是你同时又需要使用别的命令.你会想我可以到开几个终端进行登 ...

  3. js验证码倒计时

    var wait=59; function time(){ if(wait >= 0){ $("#buttons").val("" + wait + &q ...

  4. Recommender Systems基于内容的推荐

    基于内容的推荐的基本推荐思路是:用户喜欢幻想小说,这本书是幻想小说,则用户有可能喜欢这本小说 两方面要求:(1)知道用户的喜好:(2)知道物品的属性 基于内容的推荐相比协同过滤方法(个人观点):协同过 ...

  5. jmeter HTTP信息头管理器使用一例

    最近在测试过程中遇到一个问题,被测系统会检测http header:如果不包含制定内容会引发302跳转操作,从而是测试达不到效果.解决办法,增加http 信息头管理器,直接上图 此处注意: 1.此处“ ...

  6. highcharts 根据表格转化为不同的图表

    <!doctype html> <html lang="zh"> <head> <meta http-equiv="Conten ...

  7. javascript数据变量类型判断(JS变量是否是数组,是否是函数的判断)

    function isArray(o) { return Object.prototype.toString.apply(o) === “[object Array]”;}function isFun ...

  8. 手机金属外壳加工工艺:铸造、锻造、冲压、CNC

    现如今金属手机成为行业的热点,在消费电子产品中应用越来越广,本文详细介绍几种金属加工工艺及相关产品应用. 1.CNC+阳极:iPhone 5/6, HTC M7 2.锻造+CNC:华为P8,HTC M ...

  9. Android:activity跳转过渡效果

    放在startActivity(intent);后面 overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out) ...

  10. 压力测试的轻量级具体做法 Apache ab

    http://www.cnblogs.com/luminji/archive/2011/09/02/2163525.html