Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Create and drag circles in SVG

Demonstrates creating creating and dragging circles using SVG and ECMAScript.

Tested on Firefox 3 beta 4.

file: makeDragDrop.svg
<svg	xmlns="http://www.w3.org/2000/svg" width="100%"
		xmlns:xlink="http://www.w3.org/1999/xlink" >
<script><![CDATA[
var	xmlns="http://www.w3.org/2000/svg"
	xlink="http://www.w3.org/1999/xlink" 
	Root=document.documentElement
standardize(Root)

function standardize(R){
	var Attr={
		"onmouseup":"add(evt)",
		"onmousedown":"grab(evt)",
		"onmousemove":null,
		"onmouseover":"hilight(evt)",
		"onmouseout":"hilight(evt)"
	}
	assignAttr(R,Attr)
}
function hilight(evt){
	var T=evt.target
	if (T.nodeName=="rect") return
	if (evt.type=="mouseover") T.setAttributeNS(null,"stroke-opacity",1)
	else T.setAttributeNS(null,"stroke-opacity",.5)
}
function add(evt){
	if (evt.target.nodeName!="rect") return
	var C=document.createElementNS(xmlns,"circle") 
	var stroke=Color()
	var rad=10+Math.random()*50
	var Attr={
		r:rad,
		cx:evt.clientX,
		cy:evt.clientY,
		"fill": Color(),
		"fill-opacity":.75,
		"stroke": stroke,
		"stroke-opacity":.5,
		"id":stroke,
		"stroke-width":10+Math.random()*(55-rad)
	}
	assignAttr(C,Attr)
	Root.appendChild(C)
}
function grab(evt){
	var O=evt.target
	if (evt.target.nodeName=="rect") return
	var Attr={
		"onmousemove":"slide(evt,'"+O.id+"')",
		"onmouseup":"standardize(Root)"
	}
	assignAttr(Root,Attr)
}
function slide(evt,id){
	var o=document.getElementById(id)
	var c=""; if (o.nodeName=="circle") c="c"
	o.setAttributeNS(null, c+"x", evt.clientX)
	o.setAttributeNS(null, c+"y", evt.clientY)
}
function assignAttr(O,A){
	for (i in A) O.setAttributeNS(null,i, A[i])
}
function Color(){
	return "rgb("+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+")"
}
]]>
</script>
<rect width="100%" height="100%" fill="white"/>
<text font-size="12pt" x="50" y="20" id="t1">Click something to move it</text>
<text font-size="12pt" x="80" y="40" id="t2">Click nothing to add something</text>
</svg>

Source file copied from http://srufaculty.sru.edu/david.dailey/svg/makeDragDrop.svg

Circulos en Allegro

Un codigo para dibujar un circulo en allegro con degradado

#include <allegro.h>

void init();
void deinit();

int main() {
	int cont;
    
    init();
	
	
	for (cont=0;cont<255;cont++){
        circlefill(screen,320,240,255-cont,makecol(cont,0,0));
    }

	while (!key[KEY_ESC]) {
		/* put your code here */
	}

	deinit();
	return 0;
}
END_OF_MAIN();

void init() {
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

	install_timer();
	install_keyboard();
	install_mouse();
	/* add other initializations here */
}

void deinit() {
	clear_keybuf();
	/* add other deinitializations here */
}

Closest Circle Point //JavaScript Function


Given a dot and a circle, it returns the nearest dot over the circle.

[UPDATED CODE AND HELP CAN BE FOUND HERE]



//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/closest-circle-point [v1.0]

closestCirclePoint = function( px, py, x, y, ray ){
	var tg = ( x += ray, y += ray, 0 );
	return function( x, y, x0, y0 ){ return Math.sqrt( ( x -= x0 ) * x + ( y -= y0 ) * y );	}( px, py, x, y ) > ray ?
		{ x: Math.cos( tg = Math.atan2( py - y, px - x ) ) * ray + x, y: Math.sin( tg ) * ray + y }
		//{ x: ( px - x ) / ( length / ray ) + x, y: ( py - y ) / ( length / ray ) + y }
		: { x: px, y: py };
};

drawing circle function

Just to make drawing a circle easier.
Here is a function that will draw yellow circle with black outline.
# from appuifw import *
# c = Canvas()
# app.body = c

def circle(x,y,radius=5, outline=0, fill=0xffff00, width=1):
  c.ellipse((x-radius, y-radius, x+radius, y+radius), outline, fill, width)

You may use other default values.

Then I create a function to show circles randomly
import e32
from random import randint, choice

sleep = e32.ao_sleep
colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xffffff]

def rand_circle(n):
  c.clear()
  for i in range(n):
    circle(randint(0,176), randint(0,144), randint(5,20), fill=choice(colors))
    sleep(0.1)

# now show it
rand_circle(30)
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS