I've been trying to add a border to a canvas in Scalajs. I mean add a border to the Canvas itself not add a border to an object on the canvas. I am hence looking for methods on the Canvas itself not on the Graphics context from the canvas. So I have the following code which paints a brown square in the top right of the canvas:
val can: html.Canvas = document.createElement("canvas").asInstanceOf[html.Canvas]
document.body.appendChild(can)
val width = window.innerWidth -20
val height = (window.innerHeight - 80)
can.width = width.toInt
can.height = height.toInt
//val dec = new raw.CSSStyleDeclaration()
val gc = can.getContext("2d").asInstanceOf[raw.CanvasRenderingContext2D]
//display.can.setProperty("borderColor", Colour.black.hexStr)
gc.fillStyle = "#AA5500"
gc.fillRect(0, 0, 200, 200)
//dec.borderWidth = "2"
//display.can.style = dec
Looking at the documentation it seems I need to create a CSSStyleDeclaration. But as soon as I uncomment the val dec = ... line, I get a TypeError: Illegal constructor, when I run it in the Web console. Chrome's console points to the following javascript line as the problem:
new $g["CSSStyleDeclaration"]();
Edit: this works, but I'd prefer to use the proper ScalaJs methods rather resorting to dynamic:
can.asInstanceOf[scalajs.js.Dynamic].style = "border:2px solid black;"
This shows how to do the stroke with strokeRect. The canvas.strokeRect splits the stroke so that it falls partially outside and partially inside. There's an adjustment to keep all the stroke inside the canvas. You should be able to convert this to ScalaJs.
var can = document.createElement("canvas");
document.body.appendChild(can);
var width = window.innerWidth -20;
var height = (window.innerHeight - 80);
can.width = width;
can.height = height;
//val dec = new raw.CSSStyleDeclaration()
var gc = can.getContext("2d");
//display.can.setProperty("borderColor", Colour.black.hexStr)
gc.fillStyle = "#AA5500";
gc.fillRect(0, 0, 200, 200);
//dec.borderWidth = "2"
//display.can.style = dec
gc.strokeStyle = "#000000";
var strokeSize = 10;
var halfStroke = strokeSize / 2;
gc.lineWidth = strokeSize;
gc.strokeRect(0 + halfStroke,0 + halfStroke,width-strokeSize,height-strokeSize);
Related
I've recently started learning the Processing for one of my courses, and whilst I've had success in it, this one program seems to allude me. I wanted to practise drawing shapes off-screen, and then call them using the drawShape command. But obviously, that was completely wrong. When I run the HTML file that is linked to my source code, all I get is a blank screen. Note the problem is not that the file is incapable of running, I've had already made plenty of programs, but it seems this time I put in an unknown variable or command and I really don't have the experience to know what I did wrong.
// defines the color of construction
colour inside = color(0);
// defines the location of the construction
var x_pos = 0;
var y_pos = 0;
// defines the width of the construction
var totalWidth = 500;
var roofWidth = 200;
var doorWidth = totalWidth - 300;
var windowWidth = totalWidth - 320;
// defines the height of the construction
var totalHeight = 200;
var roofHeight = totalHeight-50;
var doorHeight = 0;
var windowWidth = 0;
function setup() {
// put setup code here
createCanvas(800,600);
background(0);
// defines the main body of the house & draws it of screen
body = createShape(rect, x_pos,y_pos,totalWidth,totalHeight)
square.setFill(inside);
square.setStroke(false);
endShape(CLOSE);
}
function draw() {
// put drawing code here
fill(0);
shape(body,x_pos,y_pos);
rect(x_pos,y_pos,totalWidth,totalHeight);
}
is there a way to modify this code for animate cc to make object in the stage and interact with it ?
it is a bit of pain to make drag and drop in createjs for animate cc
there is nothing in the web that describe how to do it for animate cc or flash cc even the documentation has nothing to tell about drag and drop in the canvas
//Stage
var stage = new createjs.Stage("demoCanvas");
//VARIABLES
//Drag Object Size
dragRadius = 40;
//Destination Size
destHeight = 100;
destWidth = 100;
//Circle Creation
var label = new createjs.Text("DRAG ME", "14px Lato", "#fff");
label.textAlign="center";
label.y -= 7;
var circle = new createjs.Shape();
circle.graphics.setStrokeStyle(2).beginStroke("black")
.beginFill("red").drawCircle(0,0, dragRadius);
//Drag Object Creation
//Placed inside a container to hold both label and shape
var dragger = new createjs.Container();
dragger.x = dragger.y = 100;
dragger.addChild(circle, label);
dragger.setBounds(100, 100, dragRadius*2, dragRadius*2);
//DragRadius * 2 because 2*r = width of the bounding box
var label2 = new createjs.Text("HERE", "bold 14px Lato", "#000");
label2.textAlign = "center";
label2.x += 50;
label2.y += 40;
var box = new createjs.Shape();
box.graphics.setStrokeStyle(2).beginStroke("black").rect(0, 0, destHeight, destWidth);
var destination = new createjs.Container();
destination.x = 350;
destination.y = 50;
destination.setBounds(350, 50, destHeight, destWidth);
destination.addChild(label2, box);
//DRAG FUNCTIONALITY =====================
dragger.on("pressmove", function(evt){
evt.currentTarget.x = evt.stageX;
evt.currentTarget.y = evt.stageY;
stage.update(); //much smoother because it refreshes the screen every pixel movement instead of the FPS set on the Ticker
if(intersect(evt.currentTarget, destination)){
evt.currentTarget.alpha=0.2;
box.graphics.clear();
box.graphics.setStrokeStyle(3)
.beginStroke("#0066A4")
.rect(0, 0, destHeight, destWidth);
}else{
evt.currentTarget.alpha=1;
box.graphics.clear(); box.graphics.setStrokeStyle(2).beginStroke("black").rect(0, 0, destHeight, destWidth);
}
});
//Mouse UP and SNAP====================
dragger.on("pressup", function(evt) {
if(intersect(evt.currentTarget, destination)){
dragger.x = destination.x + destWidth/2;
dragger.y = destination.y + destHeight/2;
dragger.alpha = 1;
box.graphics.clear();
box.graphics.setStrokeStyle(2).beginStroke("black").rect(0, 0, destHeight, destWidth);
stage.update(evt);
}
});
//Tests if two objects are intersecting
//Sees if obj1 passes through the first and last line of its
//bounding box in the x and y sectors
//Utilizes globalToLocal to get the x and y of obj1 in relation
//to obj2
//PRE: Must have bounds set for each object
//Post: Returns true or false
function intersect(obj1, obj2){
var objBounds1 = obj1.getBounds().clone();
var objBounds2 = obj2.getBounds().clone();
var pt = obj1.globalToLocal(objBounds2.x, objBounds2.y);
var h1 = -(objBounds1.height / 2 + objBounds2.height);
var h2 = objBounds2.width / 2;
var w1 = -(objBounds1.width / 2 + objBounds2.width);
var w2 = objBounds2.width / 2;
if(pt.x > w2 || pt.x < w1) return false;
if(pt.y > h2 || pt.y < h1) return false;
return true;
}
//Adds the object into stage
stage.addChild(destination, dragger);
stage.mouseMoveOutside = true;
stage.update();
thanks
I am not exactly sure what you are asking. The demo you showed works fine (looks like it came from this codepen), and it is not clear what you are trying to add. This demo was made directly in code, not with Animate CC - which is really good for building assets, animations, and display list structure, but you should write application code around what gets exported.
There are plenty of documentation and examples online for Drag and Drop, in the EaselJS GitHub, and EaselJS docs:
DragAndDrop demo in GitHub
Live demo on EaselJS demos page
Documentation on pressMove
Tutorial on Mouse Events which includes Drag and Drop
I recommend narrowing down what you are trying to do, show what code or approaches you have tried so far, and posting specific questions here.
Lastly, here is the first part of an ongoing series for working with Animate CC: http://blog.gskinner.com/archives/2015/04/introduction-to-the-flash-cc-html5-canvas-document.html
Cheers.
How can I get the screen size using with addon SDK ?
var w = screen.width/2;
gives me an error : Message: ReferenceError: screen is not defined
You can use the window you have associated to your add-on; it's probably safer, because it will work even if the last visible window is closed but firefox is still opened (e.g. on OS X):
const { window: { screen }} = require("sdk/addon/window");
console.log(screen.width);
This will work:
var screen = require('sdk/window/utils').getMostRecentBrowserWindow().screen;
console.log(screen.width);
If you want multi monitor support I have a script but you have to understand it. It uses XPCOM, and it needs a range. This script only checks along the x axis, you should also check along the y axis.
So this is the script here that will detect all monitors in the x plane IF it falls in the y plane of 0-20 coordintates of primary screen, I don't recommend this method.
var sm = Cc['#mozilla.org/gfx/screenmanager;1'].getService(Ci.nsIScreenManager);
function getScreens() {
var screen = null;
var screens = [];
var screenManager = sm;
var min = 0;
var max = 0;
for (x = 0; x < 15000; x += 600) {
var s = screenManager.screenForRect(x, 20, 10, 10);
if (s != screen) {
screen = s;
var left = {},
top = {},
width = {},
height = {};
screenManager.primaryScreen.GetRect(left, top, width, height);
screens.push({
width: width.value,
height: height.value,
min: min,
max: min + width.value
});
min += width.value;
}
}
return screens;
}
var screens = getScreens();
console.log('screens:', screens);
This is the method I recommend
I needed to detect all monitor dimensons and had to resort to jsctypes, if you need that its here: https://github.com/Noitidart/NativeShot/blob/master/modules/workers/MainWorker.js#L853-L1523
That code is extremely long, thats because its getting all monitors and the taking screenshots of them. So you will want to extract just the monitors part. If you need help with it I can do it for you.
I newby to Unity and I write a simple tic-tac-toe game for mobile devices.
I have troubles with GUI.Box. I try to make it stretch, but it does not work and text is clipped. My code:
GUIStyle lBoxStyle = new GUIStyle(GUI.skin.box);
lBoxStyle.stretchWidth = true;
lBoxStyle.stretchHeight = true;
lBoxStyle.wordWrap = true;
lBoxStyle.fontSize = 48;
lBoxStyle.normal.textColor = Color.black;
GUI.Box(new Rect((virtualWidth - mBoxWidth) / 2, (virtualHeight - mBoxHeight) / 2, mBoxWidth, mBoxHeight), mAlert, lBoxStyle);
How can I fix it?Also how I can reduce size if text is short?
First find out what is the size of the content with CalcSize. Then use that size to make the box tightly fitted around the content.
GUIStyle lBoxStyle = new GUIStyle(GUI.skin.box);
lBoxStyle.stretchWidth = true;
lBoxStyle.stretchHeight = true;
lBoxStyle.wordWrap = true;
lBoxStyle.fontSize = 48;
lBoxStyle.normal.textColor = Color.Black;
// Caculate the size of the content
Vector2 size = lBoxStyle.CalcSize(new GUIContent(mAlert));
// Padding if needed
// size += new Vector2(10, 10)
// Use the size as the size of the element
GUI.Box(new Rect((virtualWidth - size.x) / 2, (virtualHeight - size.y) / 2, size.x, size.y), mAlert, lBoxStyle);
I load images via LoadQueue:
this.queue = new createjs.LoadQueue(false);
I create my bitmap, this works fine:
var myImg = new createjs.Bitmap(this.queue.getResult('test-img'));
myImg.scaleX = 0.2;
myImg.scaleY = 0.2;
myImg.x = 300;
I then add a blur:
var blurFilter = new createjs.BlurFilter(5, 5, 1);
myImg.filters = [blurFilter];
var bounds = blurFilter.getBounds();
myImg.cache(-50+bounds.x, -50+bounds.y, 100+bounds.width, 100+bounds.height);
Then finish up with:
this.stage.addChild(myImg);
this.stage.update();
The problem is, as soon as I add the blur, the image no longer appears, where am I going wrong?
I implemented your code and it works well http://jsfiddle.net/k4yhz6oy/2/.
I suppose the cache area of your image is white or transparent.
myImg.cache(-50+bounds.x, -50+bounds.y, 100+bounds.width, 100+bounds.height);
Apply image bounds to cache
var imageBound = myImg.getBounds();
myImg.cache(imageBound.x, imageBound.y, imageBound.width, imageBound.height);